Skip to content
Merged
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
13 changes: 11 additions & 2 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,17 @@ struct ForwardTraversal {
return Progress::Break;
}
// Traverse condition after lowering
if (condTok && updateRecursive(condTok) == Progress::Break)
return Progress::Break;
if (condTok) {
if (updateRecursive(condTok) == Progress::Break)
return Progress::Break;

bool checkThen, checkElse;
std::tie(checkThen, checkElse) = evalCond(condTok);
if (checkElse)
// condition is false, we don't enter the loop
return Progress::Break;
}

forkScope(endBlock, allAnalysis.isModified());
if (bodyAnalysis.isModified()) {
Token* writeTok = findRange(endBlock->link(), endBlock, std::mem_fn(&ForwardAnalyzer::Action::isModified));
Expand Down
41 changes: 41 additions & 0 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class TestBufferOverrun : public TestFixture {
TEST_CASE(array_index_function_parameter);
TEST_CASE(array_index_enum_array); // #8439
TEST_CASE(array_index_container); // #9386
TEST_CASE(array_index_two_for_loops);

TEST_CASE(buffer_overrun_2_struct);
TEST_CASE(buffer_overrun_3);
Expand Down Expand Up @@ -2211,6 +2212,46 @@ class TestBufferOverrun : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void array_index_two_for_loops() {
check("bool b();\n"
"void f()\n"
"{\n"
" int val[50];\n"
" int i, sum=0;\n"
" for (i = 1; b() && i < 50; i++)\n"
" sum += val[i];\n"
" if (i < 50)\n"
" sum -= val[i];\n"
"}");
ASSERT_EQUALS("", errout.str());

check("bool b();\n"
"void f()\n"
"{\n"
" int val[50];\n"
" int i, sum=0;\n"
" for (i = 1; b() && i < 50; i++)\n"
" sum += val[i];\n"
" for (; i < 50;) {\n"
" sum -= val[i];\n"
" break;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());

check("bool b();\n"
"void f()\n"
"{\n"
" int val[50];\n"
" int i, sum=0;\n"
" for (i = 1; b() && i < 50; i++)\n"
" sum += val[i];\n"
" for (; i < 50; i++)\n"
" sum -= val[i];\n"
"}");
ASSERT_EQUALS("", errout.str());
}

void buffer_overrun_2_struct() {
check("struct ABC\n"
"{\n"
Expand Down