fix #2510 Improve check 'sizeof for array given as function argument'

This commit is contained in:
Sébastien Debrard 2011-01-26 20:08:06 +01:00
parent 5d661d25a8
commit 3e7f29d6f9
2 changed files with 46 additions and 1 deletions

View File

@ -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);
}

View File

@ -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()