Skip to content

Commit edf0fd6

Browse files
authored
Fix 15271: False positive: arrayIndexOutOfBoundsCond with ternary-initialized loop bound (#9077)
1 parent 1aea10f commit edf0fd6

4 files changed

Lines changed: 97 additions & 2 deletions

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;

‎lib/vf_analyzers.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,16 @@ struct ValueFlowAnalyzer : Analyzer {
404404
}
405405

406406
// A conditional value is split only on a condition the state covers; an unknown one may be
407-
// correlated with the assumption.
407+
// correlated with the assumption. A condition that reads the value itself guards it, so the
408+
// constraints on its other operands do not tell whether the value passes.
408409
bool canSplitConditional(const Token* condTok) const
409410
{
410411
if (!condTok)
411412
return false;
413+
if (findAstNode(condTok, [&](const Token* tok) {
414+
return match(tok);
415+
}))
416+
return false;
412417
return stateCoversCondition(condTok, pms.get(condTok, nullptr, getProgramState()));
413418
}
414419

‎test/testbufferoverrun.cpp‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class TestBufferOverrun : public TestFixture {
167167
TEST_CASE(array_index_77);
168168
TEST_CASE(array_index_78);
169169
TEST_CASE(array_index_79); // #15225
170+
TEST_CASE(array_index_80);
170171
TEST_CASE(array_index_multidim);
171172
TEST_CASE(array_index_switch_in_for);
172173
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -2074,6 +2075,59 @@ class TestBufferOverrun : public TestFixture {
20742075
ASSERT_EQUALS("", errout_str());
20752076
}
20762077

2078+
void array_index_80()
2079+
{
2080+
check("const int a[5] = {1,2,3,4,5};\n"
2081+
"const int b[5] = {6,7,8,9,0};\n"
2082+
"int f(bool c) {\n"
2083+
" int s = 0;\n"
2084+
" const int N = c ? (sizeof(a) / sizeof(a[0])) : (sizeof(b) / sizeof(b[0]));\n"
2085+
" for (int i = 0; i < N; i++) {\n"
2086+
" s += (c ? a[i] : b[i]);\n"
2087+
" if (i == 4) {}\n"
2088+
" }\n"
2089+
" return s;\n"
2090+
"}\n");
2091+
ASSERT_EQUALS("", errout_str());
2092+
2093+
check("const int a[5] = {1,2,3,4,5};\n"
2094+
"int f(bool c) {\n"
2095+
" int s = 0;\n"
2096+
" int N = 4;\n"
2097+
" if (c)\n"
2098+
" N = 5;\n"
2099+
" for (int i = 0; i < N; i++) {\n"
2100+
" s += a[i];\n"
2101+
" if (i == 4) {}\n"
2102+
" }\n"
2103+
" return s;\n"
2104+
"}\n");
2105+
ASSERT_EQUALS("", errout_str());
2106+
2107+
check("const int a[5] = {1,2,3,4,5};\n"
2108+
"int f(int N) {\n"
2109+
" int s = 0;\n"
2110+
" for (int i = 0; i < N; i++) {\n"
2111+
" s += a[i];\n"
2112+
" if (i == 4) {}\n"
2113+
" }\n"
2114+
" return s;\n"
2115+
"}\n");
2116+
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());
2129+
}
2130+
20772131
void array_index_multidim() {
20782132
check("void f()\n"
20792133
"{\n"

‎test/testvalueflow.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4766,6 +4766,41 @@ class TestValueFlow : public TestFixture {
47664766
"}\n";
47674767
ASSERT_EQUALS(false, testValueOfX(code, 5U, 0));
47684768

4769+
// a condition that reads the value itself guards it: no split
4770+
code = "void f(int n) {\n"
4771+
" for (int x = 0; x < n; x++) {\n"
4772+
" int a = x;\n"
4773+
" if (x == 4) {}\n"
4774+
" }\n"
4775+
"}\n";
4776+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 4));
4777+
ASSERT_EQUALS(false, testValueOfX(code, 3U, 5));
4778+
4779+
code = "int g();\n"
4780+
"void f(unsigned n) {\n"
4781+
" const int m = n;\n"
4782+
" int x = g();\n"
4783+
" if (x == 5) {}\n"
4784+
" if (x < m) {\n"
4785+
" int a = x;\n"
4786+
" }\n"
4787+
"}\n";
4788+
ASSERT_EQUALS(true, testValueOfX(code, 6U, 5));
4789+
ASSERT_EQUALS(false, testValueOfX(code, 7U, 5));
4790+
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+
47694804
// no fork: the main path still knows the condition is false
47704805
/*const*/ Settings s(settings);
47714806
s.vfOptions.maxForwardConditionForkDepth = 0;

0 commit comments

Comments
 (0)