diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1bb3c395f..65fe423d3 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5544,7 +5544,7 @@ struct ConditionHandler { bool dead_if = deadBranch[0]; bool dead_else = deadBranch[1]; const Token* unknownFunction = nullptr; - if (tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "while (")) + if (tok->astParent() && Token::Match(top->previous(), "while|for (")) dead_if = !isBreakScope(after); else if (!dead_if) dead_if = isReturnScope(after, &settings->library, &unknownFunction); diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index f421cabeb..f3abe0bee 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -187,6 +187,7 @@ private: TEST_CASE(array_index_negative1); TEST_CASE(array_index_negative2); // ticket #3063 TEST_CASE(array_index_negative3); + TEST_CASE(array_index_negative4); TEST_CASE(array_index_for_decr); TEST_CASE(array_index_varnames); // FP: struct member. #1576 TEST_CASE(array_index_for_continue); // for,continue @@ -2031,6 +2032,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void array_index_negative4() + { + check("void f(void) {\n" + " int buf[64]={};\n" + " int i;\n" + " for(i=0; i <16; ++i){}\n" + " for(; i < 24; ++i){ buf[i] = buf[i-16];}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void array_index_for_decr() { check("void f()\n" "{\n"