Fixed #2328 (false positive: buffer overrun (for loop with a break => the end value is not reached))

This commit is contained in:
Daniel Marjamäki 2010-12-19 10:39:43 +01:00
parent afbf5a8ef3
commit 8247270f35
2 changed files with 32 additions and 0 deletions

View File

@ -729,6 +729,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
{ {
const Token *tok2 = tok->tokAt(2); const Token *tok2 = tok->tokAt(2);
// Check if there is a break in the body..
{
const Token *bodyStart = tok->next()->link()->next();
const Token *bodyEnd = bodyStart->link();
if (Token::findmatch(bodyStart, "break ;", bodyEnd))
continue;
}
unsigned int counter_varid = 0; unsigned int counter_varid = 0;
std::string min_counter_value; std::string min_counter_value;
std::string max_counter_value; std::string max_counter_value;
@ -898,6 +906,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
{ {
const Token *tok2 = tok->tokAt(2); const Token *tok2 = tok->tokAt(2);
// Check if there is a break in the body..
{
const Token *bodyStart = tok->next()->link()->next();
const Token *bodyEnd = bodyStart->link();
if (Token::findmatch(bodyStart, "break ;", bodyEnd))
continue;
}
unsigned int counter_varid = 0; unsigned int counter_varid = 0;
std::string min_counter_value; std::string min_counter_value;
std::string max_counter_value; std::string max_counter_value;

View File

@ -112,6 +112,7 @@ private:
TEST_CASE(array_index_negative); TEST_CASE(array_index_negative);
TEST_CASE(array_index_for_decr); TEST_CASE(array_index_for_decr);
TEST_CASE(array_index_varnames); // FP: struct member. #1576 TEST_CASE(array_index_varnames); // FP: struct member. #1576
TEST_CASE(array_index_for_break); // FP: for,break
TEST_CASE(buffer_overrun_1); TEST_CASE(buffer_overrun_1);
TEST_CASE(buffer_overrun_2); TEST_CASE(buffer_overrun_2);
@ -1303,6 +1304,21 @@ private:
} }
void array_index_for_break()
{
check("void f() {\n"
" int a[2];\n"
" for (int i = 0; i <= 2; ++i) {\n"
" a[i] = 0;\n"
" if (i==1) {\n"
" break;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void buffer_overrun_1() void buffer_overrun_1()
{ {
check("void f()\n" check("void f()\n"