Buffer overruns: Fixed TODO test case

This commit is contained in:
Daniel Marjamäki 2010-10-24 11:32:27 +02:00
parent 381260dfe9
commit 306587b1d0
2 changed files with 5 additions and 8 deletions

View File

@ -458,11 +458,6 @@ void CheckBufferOverrun::parse_for_body(const Token *tok2, const ArrayInfo &arra
void CheckBufferOverrun::checkFunctionCall(const Token &tok, unsigned int par, const ArrayInfo &arrayInfo) void CheckBufferOverrun::checkFunctionCall(const Token &tok, unsigned int par, const ArrayInfo &arrayInfo)
{ {
// unknown element size : don't report errors
if (arrayInfo.element_size == 0)
return;
std::map<std::string, unsigned int> total_size; std::map<std::string, unsigned int> total_size;
total_size["fgets"] = 2; // The second argument for fgets can't exceed the total size of the array total_size["fgets"] = 2; // The second argument for fgets can't exceed the total size of the array
total_size["memcmp"] = 3; total_size["memcmp"] = 3;
@ -494,6 +489,9 @@ void CheckBufferOverrun::checkFunctionCall(const Token &tok, unsigned int par, c
std::map<std::string, unsigned int>::const_iterator it = total_size.find(tok.str()); std::map<std::string, unsigned int>::const_iterator it = total_size.find(tok.str());
if (it != total_size.end()) if (it != total_size.end())
{ {
if (arrayInfo.element_size == 0)
return;
unsigned int arg = it->second; unsigned int arg = it->second;
for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next()) for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next())
{ {
@ -716,7 +714,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
// memset, memcmp, memcpy, strncpy, fgets.. // memset, memcmp, memcpy, strncpy, fgets..
if (varid == 0) if (varid == 0)
{ {
ArrayInfo arrayInfo(0U, varnames, 1U, static_cast<unsigned int>(total_size)); ArrayInfo arrayInfo(0U, varnames, total_size / size, size);
if (Token::Match(tok, ("%var% ( " + varnames + " ,").c_str())) if (Token::Match(tok, ("%var% ( " + varnames + " ,").c_str()))
checkFunctionCall(*tok, 1, arrayInfo); checkFunctionCall(*tok, 1, arrayInfo);
if (Token::Match(tok, ("%var% ( %var% , " + varnames + " ,").c_str())) if (Token::Match(tok, ("%var% ( %var% , " + varnames + " ,").c_str()))

View File

@ -1035,8 +1035,7 @@ private:
" struct s1 obj;\n" " struct s1 obj;\n"
" x(obj.delay, 123);\n" " x(obj.delay, 123);\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:11] -> [test.cpp:6] (error) array index 4 is out of bounds", errout.str()); ASSERT_EQUALS("[test.cpp:11] -> [test.cpp:6]: (error) Array 'obj . delay[3]' index 4 out of bounds\n", errout.str());
ASSERT_EQUALS("", errout.str());
} }
void array_index_multidim() void array_index_multidim()