Fixed #3913 (boundcheck, false positive continue in loop)

This commit is contained in:
Daniel Marjamäki 2012-07-08 14:34:47 +02:00
parent 1032312fe1
commit 848fd59cbd
2 changed files with 27 additions and 1 deletions

View File

@ -224,7 +224,7 @@ static bool bailoutIfSwitch(const Token *tok, const unsigned int varid)
end = end->linkAt(2);
for (; tok != end; tok = tok->next()) {
// If scanning a "if" block then bailout for "break"
if (is_if && tok->str() == "break")
if (is_if && (tok->str() == "break" || tok->str() == "continue"))
return true;
// bailout for "return"

View File

@ -120,6 +120,7 @@ private:
TEST_CASE(array_index_for_decr);
TEST_CASE(array_index_varnames); // FP: struct member. #1576
TEST_CASE(array_index_for_break); // FP: for,break
TEST_CASE(array_index_for_continue); // for,continue
TEST_CASE(array_index_for); // FN: for,if
TEST_CASE(array_index_for_neq); // #2211: Using != in condition
TEST_CASE(array_index_for_question); // #2561: for, ?:
@ -1739,6 +1740,31 @@ private:
ASSERT_EQUALS("", errout.str());
}
void array_index_for_continue() {
// #3913
check("void f() {\n"
" int a[2];\n"
" for (int i = 0; i < 2; ++i) {\n"
" if (i == 0) {\n"
" continue;\n"
" }\n"
" a[i - 1] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int a[2];\n"
" for (int i = 0; i < 2; ++i) {\n"
" if (somecondition) {\n"
" continue;\n"
" }\n"
" a[i - 1] = 0;\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Array 'a[2]' accessed at index -1, which is out of bounds", "", errout.str());
}
void array_index_for() {
// Ticket #2370 - No false negative when there is no "break"
check("void f() {\n"