Fixed false negative: memset(foo, 0, sizeof(&foo)); is as suspicious as memset(foo, 0, sizeof(foo));

This commit is contained in:
PKEuS 2012-08-20 10:08:18 -07:00
parent 9ffc7f4c2d
commit b641a10e35
2 changed files with 10 additions and 6 deletions

View File

@ -553,15 +553,14 @@ void CheckOther::checkSizeofForPointerSize()
// This is to allow generic operations with sizeof // This is to allow generic operations with sizeof
for (; tokVar && tokVar->str() != ")" && tokVar->str() != "," && tokVar->str() != "sizeof"; tokVar = tokVar->next()) {} for (; tokVar && tokVar->str() != ")" && tokVar->str() != "," && tokVar->str() != "sizeof"; tokVar = tokVar->next()) {}
// Now check for the sizeof usage. Once here, everything using sizeof(varid) // Now check for the sizeof usage. Once here, everything using sizeof(varid) or sizeof(&varid)
// looks suspicious // looks suspicious
// Do it for first variable // Do it for first variable
if (variable && (Token::Match(tokVar, "sizeof ( %varid% )", variable->varId()) || if (variable && (Token::Match(tokVar, "sizeof ( &| %varid% )", variable->varId()) ||
Token::Match(tokVar, "sizeof %varid%", variable->varId()))) { Token::Match(tokVar, "sizeof &| %varid%", variable->varId()))) {
sizeofForPointerError(variable, variable->str()); sizeofForPointerError(variable, variable->str());
// Then do it for second - TODO: Perhaps we should invert? } else if (variable2 && (Token::Match(tokVar, "sizeof ( &| %varid% )", variable2->varId()) ||
} else if (variable2 && (Token::Match(tokVar, "sizeof ( %varid% )", variable2->varId()) || Token::Match(tokVar, "sizeof &| %varid%", variable2->varId()))) {
Token::Match(tokVar, "sizeof %varid%", variable2->varId()))) {
sizeofForPointerError(variable2, variable2->str()); sizeofForPointerError(variable2, variable2->str());
} }
} }

View File

@ -4435,6 +4435,11 @@ private:
"free(x);"); "free(x);");
ASSERT_EQUALS("[test.cpp:1]: (warning, inconclusive) Using size of pointer x instead of size of its data.\n", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (warning, inconclusive) Using size of pointer x instead of size of its data.\n", errout.str());
check(
"int *x = malloc(sizeof(&x));\n"
"free(x);");
ASSERT_EQUALS("[test.cpp:1]: (warning, inconclusive) Using size of pointer x instead of size of its data.\n", errout.str());
check( check(
"int *x = malloc(100 * sizeof(x));\n" "int *x = malloc(100 * sizeof(x));\n"
"free(x);"); "free(x);");