diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 75444720d..eaa7752fb 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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); } diff --git a/test/testother.cpp b/test/testother.cpp index 978a0e64a..19326febb 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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"