Skip to content

Commit 72e4bc9

Browse files
committed
ValueFlow: skip values that cause false assertion condition
1 parent fd85b49 commit 72e4bc9

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,19 @@ static bool valueFlowForward(Token * const startToken,
14631463
}
14641464
}
14651465

1466+
else if (Token::Match(tok2, "assert|ASSERT (") && Token::simpleMatch(tok2->linkAt(1), ") ;")) {
1467+
const Token * const arg = tok2->next()->astOperand2();
1468+
if (arg != nullptr && arg->str() != ",") {
1469+
// Should scope be skipped because variable value is checked?
1470+
for (std::list<ValueFlow::Value>::const_iterator it = values.begin(); it != values.end();) {
1471+
if (conditionIsFalse(arg, getProgramMemory(tok2, varid, *it)))
1472+
values.erase(it++);
1473+
else
1474+
++it;
1475+
}
1476+
}
1477+
}
1478+
14661479
else if (tok2->str() == "}" && indentlevel == varusagelevel) {
14671480
++number_of_if;
14681481

test/testvalueflow.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,20 @@ class TestValueFlow : public TestFixture {
17961796
"}";
17971797
ASSERT_EQUALS(false, testValueOfX(code, 6U, 5));
17981798

1799+
// assert after for loop..
1800+
code = "static void f() {\n"
1801+
" int x;\n"
1802+
" int ctls[10];\n"
1803+
" for (x = 0; x <= 10; x++) {\n"
1804+
" if (cond)\n"
1805+
" break;\n"
1806+
" }\n"
1807+
" assert(x <= 10);\n"
1808+
" ctls[x] = 123;\n" // <- x can't be 11
1809+
"}\n";
1810+
ASSERT_EQUALS(false, testValueOfX(code, 9U, 11));
1811+
1812+
17991813
// hang
18001814
code = "void f() {\n"
18011815
" for(int i = 0; i < 20; i++)\n"

0 commit comments

Comments
 (0)