Skip to content

Commit

Permalink
Partial fix for #13529 FN: zerodiv (#7191)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Jan 7, 2025
1 parent 0fdc470 commit 6f3e98e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ namespace ValueFlow
if (!isComputableValue(parent, value1))
continue;
for (const Value &value2 : parent->astOperand2()->values()) {
if (value1.path != value2.path)
if (value1.path != value2.path && value1.path != 0 && value2.path != 0)
continue;
if (!isComputableValue(parent, value2))
continue;
Expand Down
12 changes: 12 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class TestOther : public TestFixture {
TEST_CASE(zeroDiv18);
TEST_CASE(zeroDiv19);
TEST_CASE(zeroDiv20); // #11175
TEST_CASE(zeroDiv21);

TEST_CASE(zeroDivCond); // division by zero / useless condition

Expand Down Expand Up @@ -679,6 +680,17 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:4]: (error) Division by zero.\n", errout_str());
}

void zeroDiv21()
{
check("int f(int n) {\n"
" return 1 / ((1 / n) - 1);\n"
"}\n"
"int g() {\n"
" return f(1);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Division by zero.\n", errout_str());
}

void zeroDivCond() {
check("void f(unsigned int x) {\n"
" int y = 17 / x;\n"
Expand Down

0 comments on commit 6f3e98e

Please sign in to comment.