Array index out of bounds: prevent false positive when a dimension for an array is unknown

This commit is contained in:
Daniel Marjamäki 2011-08-05 13:08:48 +02:00
parent 0186fc0650
commit fd7e085c9d
2 changed files with 11 additions and 1 deletions

View File

@ -1093,6 +1093,10 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
totalElements *= arrayInfo.num(ri); totalElements *= arrayInfo.num(ri);
} }
// totalElements == 0 => Unknown size
if (totalElements == 0)
continue;
// just taking the address? // just taking the address?
const bool addr(Token::Match(tok->previous(), "[.&]") || const bool addr(Token::Match(tok->previous(), "[.&]") ||
Token::simpleMatch(tok->tokAt(-2), "& (")); Token::simpleMatch(tok->tokAt(-2), "& ("));

View File

@ -1159,7 +1159,13 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Array 'a[6][6][6]' index a[6][6][2] out of bounds\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Array 'a[6][6][6]' index a[6][6][2] out of bounds\n", errout.str());
// unknown dim..
check("void f()\n"
"{\n"
" int a[2][countof(x)] = {{1,2},{3,4}};\n"
" a[0][0] = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void array_index_switch_in_for() void array_index_switch_in_for()