From 274f4dd764d97814699520afacbc993b4cbf3cc8 Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Sat, 17 May 2025 09:34:45 +0200 Subject: [PATCH 1/4] Added regression test --- test/testother.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 0eb6218361e..2e31e08f6f6 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(zeroDiv22); TEST_CASE(zeroDivCond); // division by zero / useless condition @@ -700,6 +701,20 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:2:14]: (error) Division by zero. [zerodiv]\n", errout_str()); } + void zeroDiv22() + { + check("int main() {\n" + " int x = 20;\n" + " for (int i = 2; i >= 0; --i) {\n" + " for (int j = 1; j >= 0; --j) {\n" + " int result = x / (i * j);\n" // << + " }\n" + " }\n" + " return 0;\n" + "}"); + ASSERT_EQUALS("[test.cpp:5:28]: (error) Division by zero. [zerodiv]\n", errout_str()); + } + void zeroDivCond() { check("void f(unsigned int x) {\n" " int y = 17 / x;\n" From b37a8bbafa1c75bc35bd1be4b5f8e945e8bf46af Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Sat, 17 May 2025 10:50:45 +0200 Subject: [PATCH 2/4] Updated test accroding to review comments --- test/testother.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index 2e31e08f6f6..68241fa3538 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -60,7 +60,7 @@ class TestOther : public TestFixture { TEST_CASE(zeroDiv19); TEST_CASE(zeroDiv20); // #11175 TEST_CASE(zeroDiv21); - TEST_CASE(zeroDiv22); + TEST_CASE(zeroDivInLoop); TEST_CASE(zeroDivCond); // division by zero / useless condition @@ -701,18 +701,25 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:2:14]: (error) Division by zero. [zerodiv]\n", errout_str()); } - void zeroDiv22() + void zeroDivInLoop() { - check("int main() {\n" - " int x = 20;\n" + // single for loop + check("void f(void) {\n" + " for (int j = 1; j >= 0; --j) {\n" + " int result = 42 / j;\n" // << Division by zero when j is 0 + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:3:24]: (error) Division by zero. [zerodiv]\n", errout_str()); + + // nested for loop + check("void f(void) {\n" " for (int i = 2; i >= 0; --i) {\n" " for (int j = 1; j >= 0; --j) {\n" - " int result = x / (i * j);\n" // << + " int result = 42 / (i * j);\n" // << Division by zero when i or j is 0 " }\n" " }\n" - " return 0;\n" "}"); - ASSERT_EQUALS("[test.cpp:5:28]: (error) Division by zero. [zerodiv]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:29]: (error) Division by zero. [zerodiv]\n", errout_str()); } void zeroDivCond() { From da598aaad5c195603e536bbf0f4760f3fb61e29c Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Sat, 17 May 2025 11:01:25 +0200 Subject: [PATCH 3/4] Updated regression test --- test/testother.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index 68241fa3538..06c4a416556 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -703,7 +703,7 @@ class TestOther : public TestFixture { void zeroDivInLoop() { - // single for loop + // Single for loop check("void f(void) {\n" " for (int j = 1; j >= 0; --j) {\n" " int result = 42 / j;\n" // << Division by zero when j is 0 @@ -711,11 +711,21 @@ class TestOther : public TestFixture { "}"); ASSERT_EQUALS("[test.cpp:3:24]: (error) Division by zero. [zerodiv]\n", errout_str()); - // nested for loop + // Nested for loop where i becomes 0 check("void f(void) {\n" " for (int i = 2; i >= 0; --i) {\n" + " for (int j = 1; j > 0; --j) {\n" + " int result = 42 / (i * j);\n" // << + " }\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:4:29]: (error) Division by zero. [zerodiv]\n", errout_str()); + + // Nested for loop where j becomes 0 + check("void f(void) {\n" + " for (int i = 2; i > 0; --i) {\n" " for (int j = 1; j >= 0; --j) {\n" - " int result = 42 / (i * j);\n" // << Division by zero when i or j is 0 + " int result = 42 / (i * j);\n" // << " }\n" " }\n" "}"); From a838ed1146840932854113b002800e321d03aece Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Sun, 18 May 2025 10:04:43 +0200 Subject: [PATCH 4/4] Updated test cases according to review comments --- test/testother.cpp | 60 +++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index 06c4a416556..eb80c245444 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -703,33 +703,59 @@ class TestOther : public TestFixture { void zeroDivInLoop() { - // Single for loop + // 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 j = 1; j >= 0; --j) {\n" - " int result = 42 / j;\n" // << Division by zero when j is 0 - " }\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("[test.cpp:3:24]: (error) Division by zero. [zerodiv]\n", errout_str()); + ASSERT_EQUALS("", errout_str()); - // Nested for loop where i becomes 0 + // 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 = 1; j > 0; --j) {\n" - " int result = 42 / (i * j);\n" // << + " for (int i=2; i > 0; i--) {\n" + " for (int j=2; j >= 0; j--) {\n" + " result = 20 / (i + j);\n" " }\n" - " }\n" + " }" "}"); - ASSERT_EQUALS("[test.cpp:4:29]: (error) Division by zero. [zerodiv]\n", errout_str()); + ASSERT_EQUALS("", errout_str()); - // Nested for loop where j becomes 0 + // 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 = 1; j >= 0; --j) {\n" - " int result = 42 / (i * j);\n" // << + " for (int i=2; i > 0; i--) {\n" + " for (int j=2; j > 0; j--) {\n" + " result = 20 / (i + j);\n" " }\n" - " }\n" + " }" "}"); - ASSERT_EQUALS("[test.cpp:4:29]: (error) Division by zero. [zerodiv]\n", errout_str()); + ASSERT_EQUALS("", errout_str()); } void zeroDivCond() {