From 4cf56dac2b421a2034fe37618fbf81a6f5e903b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Debrard?= Date: Tue, 25 Jan 2011 09:57:58 +0100 Subject: [PATCH 1/4] Fix 2495 incorrect sizeof error message --- lib/checkother.cpp | 13 +++++++++++-- test/testother.cpp | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d8e065610..3d1b53443 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -95,9 +95,18 @@ 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(), ";")) && !(Token::simpleMatch(declTok->next(), ","))) + { + sizeofForArrayParameterError(tok); + } } } } diff --git a/test/testother.cpp b/test/testother.cpp index f71d7eb97..be126778b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1749,6 +1749,22 @@ private: ); 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()); + + } From 5d661d25a80c7ae1803f8321f42042c158b8bc56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Debrard?= Date: Wed, 26 Jan 2011 09:35:11 +0100 Subject: [PATCH 2/4] typo: message --- lib/checkother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d6754afe9..da92dd933 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -553,7 +553,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" From 3e7f29d6f92b32e48c7d890483be8a160ccb94c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Debrard?= Date: Wed, 26 Jan 2011 20:08:06 +0100 Subject: [PATCH 3/4] fix #2510 Improve check 'sizeof for array given as function argument' --- lib/checkother.cpp | 22 +++++++++++++++++++++- test/testother.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index da92dd933..33f6f7641 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -157,7 +157,27 @@ void CheckOther::checkSizeofForArrayParameter() { declTok = declTok->next()->link(); } - if (!(Token::Match(declTok->next(), "= %str%")) && !(Token::simpleMatch(declTok->next(), "= {")) && !(Token::simpleMatch(declTok->next(), ";")) && !(Token::simpleMatch(declTok->next(), ","))) + 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); } diff --git a/test/testother.cpp b/test/testother.cpp index a578b8a7b..bdec704e3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1774,6 +1774,31 @@ private: ); 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() From 078c36921da4c650f0cb4f1edc7e6a56e7126d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Debrard?= Date: Wed, 26 Jan 2011 20:10:56 +0100 Subject: [PATCH 4/4] runastyle --- lib/checkother.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 33f6f7641..6e74c7cc6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -157,12 +157,12 @@ void CheckOther::checkSizeofForArrayParameter() { declTok = declTok->next()->link(); } - if (!(Token::Match(declTok->next(), "= %str%")) && !(Token::simpleMatch(declTok->next(), "= {")) && !(Token::simpleMatch(declTok->next(), ";"))) + 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, ";")) + while (!Token::simpleMatch(declTok, ";")) { if (Token::simpleMatch(declTok, ")")) {