From ed477ceb7414689858874cd2dfc5c2a03a4064a8 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Thu, 14 Mar 2013 09:27:42 -0700 Subject: [PATCH] Fixed false negative mentioned in #4354. --- lib/checkclass.cpp | 10 +++++++++- test/testclass.cpp | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index b532862df..85e7ed3f6 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -200,7 +200,15 @@ void CheckClass::copyconstructors() for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->type == Function::eConstructor && func->functionScope) { - for (const Token* tok = func->functionScope->classStart; tok!=func->functionScope->classEnd; tok=tok->next()) { + const Token* tok = func->functionScope->classDef->linkAt(1); + for (const Token* const end = func->functionScope->classStart; tok != end; tok = tok->next()) { + if (Token::Match(tok, "%var% ( new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) { + const Variable* var = tok->variable(); + if (var && var->isPointer() && var->scope() == &*scope) + allocatedVars[tok->varId()] = tok; + } + } + for (const Token* const end = func->functionScope->classEnd; tok != end; tok = tok->next()) { if (Token::Match(tok, "%var% = new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) { const Variable* var = tok->variable(); if (var && var->isPointer() && var->scope() == &*scope) diff --git a/test/testclass.cpp b/test/testclass.cpp index dffee9851..e6bf5c563 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -374,7 +374,7 @@ private: " p = malloc(100);\n" " }\n" "};"); - TODO_ASSERT_EQUALS("[test.cpp:2]: (style) 'class F' does not have a copy constructor which is required since the class contains a pointer to allocated memory.\n", "", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:2]: (style) 'class F' does not have a copy constructor which is recommended since the class contains a pointer to allocated memory.\n", "", errout.str()); checkCopyConstructor("class F {\n" " char *p;\n" @@ -384,6 +384,12 @@ private: " F(F& f);\n" // non-copyable "};"); ASSERT_EQUALS("", errout.str()); + + checkCopyConstructor("class F {\n" + " char *p;\n" + " F() : p(malloc(100)) {}\n" + "};"); + ASSERT_EQUALS("[test.cpp:1]: (style) 'class F' does not have a copy constructor which is recommended since the class contains a pointer to allocated memory.\n", errout.str()); }