Fix #946 (False positive: Buffer access out-of-bounds)

http://sourceforge.net/apps/trac/cppcheck/ticket/946
This commit is contained in:
Reijo Tomperi 2009-11-12 23:24:44 +02:00
parent b2cec721c0
commit 0518eed937
2 changed files with 18 additions and 2 deletions

View File

@ -716,6 +716,7 @@ void CheckBufferOverrun::checkStructVariable()
continue;
const char *varname[3] = {0, 0, 0};
const unsigned int varId = tok2->tokAt(ivar)->varId();
varname[1] = tok2->strAt(ivar);
int arrsize = std::atoi(tok2->strAt(ivar + 2));
int total_size = arrsize * _tokenizer->sizeOfType(tok2->tokAt(1));
@ -737,8 +738,7 @@ void CheckBufferOverrun::checkStructVariable()
if (Token::simpleMatch(tok4, ") {"))
{
const char *names[2] = {varname[1], 0};
checkScope(tok4->tokAt(2), names, arrsize, total_size, 0);
checkScope(tok4->tokAt(2), 0, arrsize, total_size, varId);
break;
}
}

View File

@ -89,6 +89,7 @@ private:
TEST_CASE(array_index_18);
TEST_CASE(array_index_19);
TEST_CASE(array_index_20);
TEST_CASE(array_index_21);
TEST_CASE(array_index_multidim);
TEST_CASE(buffer_overrun_1);
@ -653,6 +654,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void array_index_21()
{
check("class A {\n"
" int indices[2];\n"
" void foo(int indices[3]);\n"
"};\n"
"\n"
"void A::foo(int indices[3]) {\n"
" for(int j=0; j<3; ++j) {\n"
" int b = indices[j];\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void array_index_multidim()
{
check("void f()\n"