From 9943262da05073661623f891452ed2ac1d974610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 4 Feb 2010 19:40:35 +0100 Subject: [PATCH] Fixed #1287 (Use of memset on struct - std template types not handled) --- lib/checkclass.cpp | 18 ++++++++---------- test/testclass.cpp | 23 ++++++++++++++++++----- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index c14985198..1f418d7d9 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -745,16 +745,8 @@ void CheckClass::noMemset() if (!(type && type[0])) continue; - // Warn if type is a class.. - const std::string pattern1(std::string("class ") + type); - if (Token::findmatch(_tokenizer->tokens(), pattern1.c_str())) - { - memsetClassError(tok, tok->str()); - continue; - } - - // Warn if type is a struct that contains any std::* - const std::string pattern2(std::string("struct ") + type + " {"); + // Warn if type is a class or struct that contains any std::* variables + const std::string pattern2(std::string("struct|class ") + type + " {"); for (const Token *tstruct = Token::findmatch(_tokenizer->tokens(), pattern2.c_str()); tstruct; tstruct = tstruct->next()) { if (tstruct->str() == "}") @@ -765,6 +757,12 @@ void CheckClass::noMemset() memsetStructError(tok, tok->str(), tstruct->strAt(2)); break; } + + if (Token::Match(tstruct, "std :: %type% < %type% > %var% ;")) + { + memsetStructError(tok, tok->str(), tstruct->strAt(2)); + break; + } } } } diff --git a/test/testclass.cpp b/test/testclass.cpp index e495f72d4..930deea1f 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -1436,18 +1436,18 @@ private: "};\n" "void f()\n" "{\n" - " A fail;\n" - " memset(&fail, 0, sizeof(A));\n" + " A a;\n" + " memset(&a, 0, sizeof(A));\n" "}\n"); - ASSERT_EQUALS("[test.cpp:7]: (error) Using 'memset' on class\n", errout.str()); + ASSERT_EQUALS("", errout.str()); checkNoMemset("struct A\n" "{\n" "};\n" "void f()\n" "{\n" - " struct A fail;\n" - " memset(&fail, 0, sizeof(A));\n" + " struct A a;\n" + " memset(&a, 0, sizeof(A));\n" "}\n"); ASSERT_EQUALS("", errout.str()); } @@ -1481,6 +1481,19 @@ private: ASSERT_EQUALS("[test.cpp:10]: (error) Using 'memset' on struct that contains a 'std::string'\n", errout.str()); } + void memsetVector() + { + checkNoMemset("struct A\n" + "{ std::vector ints; }\n" + "\n" + "void f()\n" + "{\n" + " A a;\n" + " memset(a, 0, sizeof(A));\n" + "}"); + ASSERT_EQUALS("[test.cpp:7]: (error) Using 'memset' on struct that contains a 'std::vector'\n", errout.str()); + } + void checkThisSubtraction(const char code[]) {