Fix #868 (False positive - buffer access out of bounds in for loop)

http://sourceforge.net/apps/trac/cppcheck/ticket/868
This commit is contained in:
Reijo Tomperi 2009-10-29 16:04:23 +02:00
parent 38e1a0e03d
commit 504ae8e22d
2 changed files with 18 additions and 2 deletions

View File

@ -376,6 +376,9 @@ void CheckBufferOverrun::checkScope(const Token *tok, const char *varname[], con
}
std::ostringstream pattern;
if (varid > 0)
pattern << "%varid% [ " << strindex << " ]";
else
pattern << varnames << " [ " << strindex << " ]";
int indentlevel2 = 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;

View File

@ -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"