diff --git a/test/testother.cpp b/test/testother.cpp index 0eb6218361e..eb80c245444 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -60,6 +60,7 @@ class TestOther : public TestFixture { TEST_CASE(zeroDiv19); TEST_CASE(zeroDiv20); // #11175 TEST_CASE(zeroDiv21); + TEST_CASE(zeroDivInLoop); TEST_CASE(zeroDivCond); // division by zero / useless condition @@ -700,6 +701,63 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:2:14]: (error) Division by zero. [zerodiv]\n", errout_str()); } + void zeroDivInLoop() + { + // For loop where j becomes 0 + check("void foo(int i) {\n" + " for (int j=2; j >= 0; j--)\n" + " result = 20 / (i * j);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3:22]: (error) Division by zero. [zerodiv]\n", errout_str()); + + // No warning is expected, because j will never become 0 + check("void foo(int i) {\n" + " for (int j=2; j > 0; j--)\n" + " result = 20 / (i * j);\n" + "}"); + ASSERT_EQUALS("", errout_str()); + + // Nested for loop where i and j becomes 0 simultaneously + check("void f(void) {\n" // # 13874 + " for (int i=2; i >= 0; i--) {\n" + " for (int j=2; j >= 0; j--) {\n" + " result = 20 / (i + j);\n" + " }\n" + " }" + "}"); + TODO_ASSERT_EQUALS("[test.cpp:4:25]: (error) Division by zero. [zerodiv]\n", "", errout_str()); + + // No warning is expected, as j cannot be 0 + check("void f(void) {\n" + " for (int i=2; i >= 0; i--) {\n" + " for (int j=2; j > 0; j--) {\n" + " result = 20 / (i + j);\n" + " }\n" + " }" + "}"); + ASSERT_EQUALS("", errout_str()); + + // No warning is expected, as i cannot be 0 + check("void f(void) {\n" + " for (int i=2; i > 0; i--) {\n" + " for (int j=2; j >= 0; j--) {\n" + " result = 20 / (i + j);\n" + " }\n" + " }" + "}"); + ASSERT_EQUALS("", errout_str()); + + // No warning is expected, as neither i nor j can be 0 + check("void f(void) {\n" + " for (int i=2; i > 0; i--) {\n" + " for (int j=2; j > 0; j--) {\n" + " result = 20 / (i + j);\n" + " }\n" + " }" + "}"); + ASSERT_EQUALS("", errout_str()); + } + void zeroDivCond() { check("void f(unsigned int x) {\n" " int y = 17 / x;\n"