buffer overruns: added simple support for initialized array

This commit is contained in:
Daniel Marjamäki 2009-02-12 19:11:52 +00:00
parent 62a49282eb
commit 4f121daca4
2 changed files with 14 additions and 1 deletions

View File

@ -405,7 +405,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_GlobalAndLocalVariable()
unsigned int varid = 0;
int nextTok = 0;
if (Token::Match(tok, "%type% %var% [ %num% ] ;"))
if (Token::Match(tok, "%type% %var% [ %num% ] [;=]"))
{
varname[0] = tok->strAt(1);
size = std::strtoul(tok->strAt(3), NULL, 10);

View File

@ -94,6 +94,8 @@ private:
TEST_CASE(varid1);
TEST_CASE(varid2);
TEST_CASE(assign1);
}
@ -492,6 +494,17 @@ private:
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
void assign1()
{
check("char str[3] = {'a', 'b', 'c'};\n"
"\n"
"void foo()\n"
"{\n"
" str[3] = 0;\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:5]: (all) Array index out of bounds\n"), errout.str());
}
};
REGISTER_TEST(TestBufferOverrun)