Skip to content

Added regression test for ticket #13474 #7536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down
Loading