Skip to content

Commit

Permalink
Fix #12318 FP negativeIndex with do while loop
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 24, 2024
1 parent 6faed30 commit 7882142
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ namespace {
if (!checkThen && !checkElse && !isDoWhile && stopOnCondition(condTok) && stopUpdates())
return Break(Analyzer::Terminate::Conditional);
// condition is false, we don't enter the loop
if (checkElse)
if (checkElse && !isDoWhile)
return Progress::Continue;
if (checkThen || isDoWhile) {
// Since we are re-entering the loop then assume the condition is true to update the state
Expand Down
12 changes: 12 additions & 0 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,18 @@ class TestBufferOverrun : public TestFixture {
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (warning) Either the condition 's>sizeof(a)' is redundant or the array 'a[16]' is accessed at index 16, which is out of bounds.\n",
errout_str());

check("void f(int fd) {\n" // #12318
" char buf[10];\n"
" int size = 0;\n"
" int pos = -1;\n"
" do {\n"
" pos++;\n"
" size = read(fd, &buf[pos], 1);\n"
" } while (size > 0);\n"
" buf[pos] = '\\0';\n"
"}\n");
ASSERT_EQUALS("", errout_str());

}

void array_index_valueflow_pointer() {
Expand Down

0 comments on commit 7882142

Please sign in to comment.