Skip to content

Commit 950f5ec

Browse files
committed
Use the entire expression
1 parent 182fabe commit 950f5ec

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎lib/programmemory.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ static void programMemoryParseCondition(ProgramMemory& pm,
530530
return;
531531
if (!truevalue.isIntValue())
532532
return;
533-
if (endTok && changed(vartok, tok->next(), endTok))
533+
// The other operand is evaluated with its current value, so it must be unchanged as well
534+
if (endTok && changed(tok, tok->next(), endTok))
534535
return;
535536
const bool impossible = (tok->str() == "==" && !then) || (tok->str() == "!=" && then);
536537
ValueFlow::Value& v = then ? truevalue : falsevalue;

‎test/testbufferoverrun.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,18 @@ class TestBufferOverrun : public TestFixture {
21142114
" return s;\n"
21152115
"}\n");
21162116
ASSERT_EQUALS("", errout_str());
2117+
2118+
check("const int a[5] = {1,2,3,4,5};\n"
2119+
"int f(int N, int i) {\n"
2120+
" if (i < N) {\n"
2121+
" if (i == 4) {}\n"
2122+
" i++;\n"
2123+
" if (i < N)\n"
2124+
" return a[i];\n"
2125+
" }\n"
2126+
" return 0;\n"
2127+
"}\n");
2128+
ASSERT_EQUALS("", errout_str());
21172129
}
21182130

21192131
void array_index_multidim() {

‎test/testvalueflow.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,6 +4788,19 @@ class TestValueFlow : public TestFixture {
47884788
ASSERT_EQUALS(true, testValueOfX(code, 6U, 5));
47894789
ASSERT_EQUALS(false, testValueOfX(code, 7U, 5));
47904790

4791+
// the enclosing condition held for the value before it was changed
4792+
code = "void f(int n, int x) {\n"
4793+
" if (x < n) {\n"
4794+
" if (x == 4) {}\n"
4795+
" x++;\n"
4796+
" if (x < n) {\n"
4797+
" int a = x;\n"
4798+
" }\n"
4799+
" }\n"
4800+
"}\n";
4801+
ASSERT_EQUALS(true, testValueOfX(code, 5U, 5));
4802+
ASSERT_EQUALS(false, testValueOfX(code, 6U, 5));
4803+
47914804
// no fork: the main path still knows the condition is false
47924805
/*const*/ Settings s(settings);
47934806
s.vfOptions.maxForwardConditionForkDepth = 0;

0 commit comments

Comments
 (0)