Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5422,6 +5422,11 @@ static void valueFlowForLoopSimplifyAfter(Token* fortok, nonneg int varid, const
endToken = fortok->scope()->bodyEnd;

Token* blockTok = fortok->linkAt(1)->linkAt(1);
if (const Token* escape = findEscapeStatement(blockTok->scope(), settings.library)) {
if (settings.debugwarnings)
bailout(tokenlist, errorLogger, escape, "For loop variable bailout on escape statement");
return;
}
if (blockTok != endToken) {
ValueFlow::Value v{num};
v.errorPath.emplace_back(fortok,"After for loop, " + var->name() + " has value " + v.infoString());
Expand Down
4 changes: 4 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,10 @@ class TestTokenizer : public TestFixture {
"return ; } }\n\n"
"return ;\n"
"}", tokenizeAndStringify(code));
ASSERT_EQUALS_WITHOUT_LINENUMBERS(
"[test.cpp:5:1]: (debug) valueflow.cpp:5427:valueFlowForLoopSimplifyAfter bailout: For loop variable bailout on escape statement [valueFlowBailout]\n"
"[test.cpp:5:1]: (debug) valueflow.cpp:5427:valueFlowForLoopSimplifyAfter bailout: For loop variable bailout on escape statement [valueFlowBailout]\n",
errout_str());
}

void ifAddBraces7() {
Expand Down
12 changes: 12 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5143,6 +5143,18 @@ class TestValueFlow : public TestFixture {
++it;
ASSERT_EQUALS(5, it->intvalue);
ASSERT(it->isImpossible());

code = "int a[5];\n" // #14998
"int f() {\n"
" int i;\n"
" for (i = 0; true; ++i) {\n"
" if (i == 4)\n"
" break;\n"
" }\n"
" return a[i];\n"
"}\n";
values = tokenValues(code, "i ]");
ASSERT(values.empty());
}

void valueFlowSubFunction() {
Expand Down
Loading