diff --git a/lib/checksizeof.cpp b/lib/checksizeof.cpp index a40a79c9c..c071a8fc5 100644 --- a/lib/checksizeof.cpp +++ b/lib/checksizeof.cpp @@ -190,10 +190,10 @@ void CheckSizeof::checkSizeofForPointerSize() // looks suspicious // Do it for first variable 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()); } 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()); } } diff --git a/test/testsizeof.cpp b/test/testsizeof.cpp index c6b83ba55..626d18bfb 100644 --- a/test/testsizeof.cpp +++ b/test/testsizeof.cpp @@ -36,6 +36,7 @@ private: TEST_CASE(sizeofsizeof); TEST_CASE(sizeofCalculation); TEST_CASE(checkPointerSizeof); + TEST_CASE(checkPointerSizeofStruct); TEST_CASE(sizeofDivisionMemset); TEST_CASE(sizeofForArrayParameter); TEST_CASE(sizeofForNumericParameter); @@ -502,6 +503,14 @@ private: 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() { check("void foo(memoryMapEntry_t* entry, memoryMapEntry_t* memoryMapEnd) {\n" " memmove(entry, entry + 1, (memoryMapEnd - entry) / sizeof(entry));\n"