diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 76af4e0a5..6e74c7cc6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -149,9 +149,38 @@ void CheckOther::checkSizeofForArrayParameter() const Token *declTok = Token::findmatch(_tokenizer->tokens(), "%varid%", tok->tokAt(tokIdx)->varId()); if (declTok) { - if ((Token::simpleMatch(declTok->next(), "[")) && !(Token::Match(declTok->next()->link(), "] = %str%")) && !(Token::simpleMatch(declTok->next()->link(), "] = {")) && !(Token::simpleMatch(declTok->next()->link(), "] ;"))) + if (Token::simpleMatch(declTok->next(), "[")) { - sizeofForArrayParameterError(tok); + declTok = declTok->next()->link(); + // multidimensional array + while (Token::simpleMatch(declTok->next(), "[")) + { + declTok = declTok->next()->link(); + } + if (!(Token::Match(declTok->next(), "= %str%")) && !(Token::simpleMatch(declTok->next(), "= {")) && !(Token::simpleMatch(declTok->next(), ";"))) + { + if (Token::simpleMatch(declTok->next(), ",")) + { + declTok = declTok->next(); + while (!Token::simpleMatch(declTok, ";")) + { + if (Token::simpleMatch(declTok, ")")) + { + sizeofForArrayParameterError(tok); + break; + } + if (Token::Match(declTok, "(|[|{")) + { + declTok = declTok->link(); + } + declTok = declTok->next(); + } + } + } + if (Token::simpleMatch(declTok->next(), ")")) + { + sizeofForArrayParameterError(tok); + } } } } @@ -544,7 +573,7 @@ void CheckOther::sizeofForArrayParameterError(const Token *tok) "returns the size of pointer.\n" "Giving array as function parameter and then using sizeof-operator for the array " "argument. In this case the sizeof-operator returns the size of pointer (in the " - " system). It does not return the size of the whole array in bytes as might be " + "system). It does not return the size of the whole array in bytes as might be " "expected. For example, this code:\n" " int f(char a[100]) {\n" " return sizeof(a);\n" diff --git a/test/testother.cpp b/test/testother.cpp index 8546c5d83..bdec704e3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1751,6 +1751,54 @@ private: "}\n" ); ASSERT_EQUALS("", errout.str()); + + // ticket 2495 + check("void f() {\n" + " static float col[][3]={\n" + " {1,0,0},\n" + " {0,0,1},\n" + " {0,1,0},\n" + " {1,0,1},\n" + " {1,0,1},\n" + " {1,0,1},\n" + " };\n" + " const int COL_MAX=sizeof(col)/sizeof(col[0]);\n" + "}\n" + ); + ASSERT_EQUALS("", errout.str()); + + // ticket 155 + check("void f() {\n" + " char buff1[1024*64],buff2[sizeof(buff1)*2];\n" + "}\n" + ); + ASSERT_EQUALS("", errout.str()); + + // ticket 2510 + check("void f( int a[], int b) {\n" + " std::cout << sizeof(a) / sizeof(int) << std::endl;\n" + "}\n" + ); + ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof for array given as " + "function argument returns the size of pointer.\n", errout.str()); + + // ticket 2510 + check("void f( int a[3] , int b[2] ) {\n" + " std::cout << sizeof(a) / sizeof(int) << std::endl;\n" + "}\n" + ); + ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof for array given as " + "function argument returns the size of pointer.\n", errout.str()); + + // ticket 2510 + check("void f() {\n" + " char buff1[1024*64],buff2[sizeof(buff1)*(2+1)];\n" + "}\n" + ); + ASSERT_EQUALS("", errout.str()); + + + } void clarifyCalculation()