diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 3b5f91947..358318cf1 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -376,7 +376,10 @@ void CheckBufferOverrun::checkScope(const Token *tok, const char *varname[], con } std::ostringstream pattern; - pattern << varnames << " [ " << strindex << " ]"; + if (varid > 0) + pattern << "%varid% [ " << strindex << " ]"; + else + pattern << varnames << " [ " << strindex << " ]"; int indentlevel2 = 0; while ((tok2 = tok2->next()) != 0) @@ -400,7 +403,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const char *varname[], con break; } - if (Token::Match(tok2, pattern.str().c_str()) && condition_out_of_bounds) + if (condition_out_of_bounds && Token::Match(tok2, pattern.str().c_str(), varid)) { bufferOverrun(tok2); break; diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 7aeacbae3..74076903c 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -89,6 +89,7 @@ private: TEST_CASE(array_index_17); TEST_CASE(array_index_18); TEST_CASE(array_index_19); + TEST_CASE(array_index_20); TEST_CASE(array_index_multidim); TEST_CASE(buffer_overrun_1); @@ -624,6 +625,18 @@ private: ASSERT_EQUALS("[test.cpp:4]: (error) Array index out of bounds\n", errout.str()); } + void array_index_20() + { + check("void f()\n" + "{\n" + " char a[8];\n" + " int b[10];\n" + " for ( int i = 0; i < 9; i++ )\n" + " b[i] = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void array_index_multidim() { check("void f()\n"