ticket 155

This commit is contained in:
Sébastien Debrard 2011-01-22 10:51:00 +01:00
parent 53c36f53ef
commit 8d97080ab8
2 changed files with 24 additions and 21 deletions

View File

@ -83,31 +83,18 @@ void CheckOther::checkSizeofWithSilentArrayPointer()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "sizeof ( %var% )"))
if (Token::Match(tok, "sizeof ( %var% )") || Token::Match(tok, "sizeof %var% "))
{
if (tok->tokAt(2)->varId() > 0)
int tokIdx = 1;
if (tok->tokAt(tokIdx)->str() == "(") {
++tokIdx;
}
if (tok->tokAt(tokIdx)->varId() > 0)
{
const Token *declTok = Token::findmatch(_tokenizer->tokens(), "%varid% [", tok->tokAt(2)->varId());
const Token *declTok = Token::findmatch(_tokenizer->tokens(), "%varid% [", tok->tokAt(tokIdx)->varId());
if (declTok)
{
int idx = 2;
bool bracket = false;
while (!Token::simpleMatch(declTok->tokAt(idx), "]") || bracket)
{
if (Token::simpleMatch(declTok->tokAt(idx), "["))
{
bracket = true;
}
else
{
if (Token::simpleMatch(declTok->tokAt(idx), "]"))
{
bracket = false;
}
}
++idx;
}
if (!(Token::simpleMatch(declTok->tokAt(idx), "] = {")) && !(Token::simpleMatch(declTok->tokAt(idx), "] ;")))
if (!(Token::simpleMatch(declTok->next()->link(), "] = {")) && !(Token::simpleMatch(declTok->next()->link(), "] ;")))
{
sizeofWithSilentArrayPointerError(tok);
}

View File

@ -1681,6 +1681,16 @@ private:
);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" unsigned int a = { 2 };\n"
" unsigned int b[] = { 0 };\n"
" int c[a[b[0]]];\n"
" std::cout << sizeof(c) / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" unsigned int a[] = { 1 };\n"
" unsigned int b = 2;\n"
@ -1710,6 +1720,12 @@ private:
);
ASSERT_EQUALS("[test.cpp:2]: (error) silent pointer of array is passed as parameter to the function sizeof.\n", errout.str());
check("void f( int a[]) {\n"
" std::cout << sizeof a / sizeof(int) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (error) silent pointer of array is passed as parameter to the function sizeof.\n", errout.str());
check("void f( int a[3] ) {\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"