Fix ticket #445 (simple to avoid false positive for buffer overflow)

http://sourceforge.net/apps/trac/cppcheck/ticket/445
Simple bailout in case "if" is found inside the for-loop.
This commit is contained in:
Reijo Tomperi 2009-06-30 00:42:46 +03:00
parent d029a50833
commit 751a31ed91
2 changed files with 20 additions and 1 deletions

View File

@ -249,6 +249,12 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope(const Token *tok, co
break; break;
} }
if (tok2->str() == "if")
{
// Bailout
break;
}
if (Token::Match(tok2, pattern.str().c_str())) if (Token::Match(tok2, pattern.str().c_str()))
{ {
bufferOverrun(tok2); bufferOverrun(tok2);

View File

@ -82,6 +82,7 @@ private:
TEST_CASE(array_index_10); TEST_CASE(array_index_10);
TEST_CASE(array_index_11); TEST_CASE(array_index_11);
TEST_CASE(array_index_12); TEST_CASE(array_index_12);
TEST_CASE(array_index_13);
TEST_CASE(buffer_overrun_1); TEST_CASE(buffer_overrun_1);
TEST_CASE(buffer_overrun_2); TEST_CASE(buffer_overrun_2);
@ -417,7 +418,19 @@ private:
ASSERT_EQUALS("[test.cpp:10]: (all) Array index out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:10]: (all) Array index out of bounds\n", errout.str());
} }
void array_index_13()
{
check("void f()\n"
"{\n"
" char buf[10];\n"
" for (int i = 0; i < 100; i++)\n"
" {\n"
" if (i < 10)\n"
" int x = buf[i];\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void buffer_overrun_1() void buffer_overrun_1()
{ {