Fix FP if sizeof is used without parentheses on struct members

Right now we only support checking basic pointer types.
Pointers inside structs are not supported yet.

Consider the tokens "foo@1 . bar@2",
Token::Match( "sizeof ( %varid% )" ) won't match it.
Token::Match( "sizeof %varid%" ) did match it -> FP.
This commit is contained in:
Thomas Jarosch 2014-12-22 09:28:54 +01:00
parent 93ac5a41cd
commit dca65ce3da
2 changed files with 11 additions and 2 deletions

View File

@ -190,10 +190,10 @@ void CheckSizeof::checkSizeofForPointerSize()
// looks suspicious // looks suspicious
// Do it for first variable // Do it for first variable
if (variable && (Token::Match(tokSize, "sizeof ( &| %varid% )", variable->varId()) || if (variable && (Token::Match(tokSize, "sizeof ( &| %varid% )", variable->varId()) ||
Token::Match(tokSize, "sizeof &| %varid%", variable->varId()))) { Token::Match(tokSize, "sizeof &| %varid% !!.", variable->varId()))) {
sizeofForPointerError(variable, variable->str()); sizeofForPointerError(variable, variable->str());
} else if (variable2 && (Token::Match(tokSize, "sizeof ( &| %varid% )", variable2->varId()) || } else if (variable2 && (Token::Match(tokSize, "sizeof ( &| %varid% )", variable2->varId()) ||
Token::Match(tokSize, "sizeof &| %varid%", variable2->varId()))) { Token::Match(tokSize, "sizeof &| %varid% !!.", variable2->varId()))) {
sizeofForPointerError(variable2, variable2->str()); sizeofForPointerError(variable2, variable2->str());
} }
} }

View File

@ -36,6 +36,7 @@ private:
TEST_CASE(sizeofsizeof); TEST_CASE(sizeofsizeof);
TEST_CASE(sizeofCalculation); TEST_CASE(sizeofCalculation);
TEST_CASE(checkPointerSizeof); TEST_CASE(checkPointerSizeof);
TEST_CASE(checkPointerSizeofStruct);
TEST_CASE(sizeofDivisionMemset); TEST_CASE(sizeofDivisionMemset);
TEST_CASE(sizeofForArrayParameter); TEST_CASE(sizeofForArrayParameter);
TEST_CASE(sizeofForNumericParameter); TEST_CASE(sizeofForNumericParameter);
@ -502,6 +503,14 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void checkPointerSizeofStruct() {
check("void f() {\n"
" struct foo *ptr;\n"
" memset( ptr->bar, 0, sizeof ptr->bar );\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void sizeofDivisionMemset() { void sizeofDivisionMemset() {
check("void foo(memoryMapEntry_t* entry, memoryMapEntry_t* memoryMapEnd) {\n" check("void foo(memoryMapEntry_t* entry, memoryMapEntry_t* memoryMapEnd) {\n"
" memmove(entry, entry + 1, (memoryMapEnd - entry) / sizeof(entry));\n" " memmove(entry, entry + 1, (memoryMapEnd - entry) / sizeof(entry));\n"