From dca65ce3da2e98d2d6a1405b7b78c306c90bdc51 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Mon, 22 Dec 2014 09:28:54 +0100 Subject: [PATCH] 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. --- lib/checksizeof.cpp | 4 ++-- test/testsizeof.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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"