Skip to content

Commit 093e55f

Browse files
Fix #14998 FP arrayIndexOutOfBounds after loop (#8818)
1 parent 18d6c1d commit 093e55f

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5422,6 +5422,11 @@ static void valueFlowForLoopSimplifyAfter(Token* fortok, nonneg int varid, const
54225422
endToken = fortok->scope()->bodyEnd;
54235423

54245424
Token* blockTok = fortok->linkAt(1)->linkAt(1);
5425+
if (const Token* escape = findEscapeStatement(blockTok->scope(), settings.library)) {
5426+
if (settings.debugwarnings)
5427+
bailout(tokenlist, errorLogger, escape, "For loop variable bailout on escape statement");
5428+
return;
5429+
}
54255430
if (blockTok != endToken) {
54265431
ValueFlow::Value v{num};
54275432
v.errorPath.emplace_back(fortok,"After for loop, " + var->name() + " has value " + v.infoString());

test/testtokenize.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,10 @@ class TestTokenizer : public TestFixture {
13401340
"return ; } }\n\n"
13411341
"return ;\n"
13421342
"}", tokenizeAndStringify(code));
1343+
ASSERT_EQUALS_WITHOUT_LINENUMBERS(
1344+
"[test.cpp:5:1]: (debug) valueflow.cpp:5427:valueFlowForLoopSimplifyAfter bailout: For loop variable bailout on escape statement [valueFlowBailout]\n"
1345+
"[test.cpp:5:1]: (debug) valueflow.cpp:5427:valueFlowForLoopSimplifyAfter bailout: For loop variable bailout on escape statement [valueFlowBailout]\n",
1346+
errout_str());
13431347
}
13441348

13451349
void ifAddBraces7() {

test/testvalueflow.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5143,6 +5143,18 @@ class TestValueFlow : public TestFixture {
51435143
++it;
51445144
ASSERT_EQUALS(5, it->intvalue);
51455145
ASSERT(it->isImpossible());
5146+
5147+
code = "int a[5];\n" // #14998
5148+
"int f() {\n"
5149+
" int i;\n"
5150+
" for (i = 0; true; ++i) {\n"
5151+
" if (i == 4)\n"
5152+
" break;\n"
5153+
" }\n"
5154+
" return a[i];\n"
5155+
"}\n";
5156+
values = tokenValues(code, "i ]");
5157+
ASSERT(values.empty());
51465158
}
51475159

51485160
void valueFlowSubFunction() {

0 commit comments

Comments
 (0)