From fa94f2e0f4bc33eec5b7b5755a58a639bdcea671 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Sun, 19 Jul 2015 17:37:50 +0200 Subject: [PATCH] Ticket #6648: Properly handle variables that have been deallocated and whose _address_ is taken after in CheckLeakAutoVar. --- lib/checkleakautovar.cpp | 2 +- test/testleakautovar.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 8f2b9c6ae..6e639246f 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -160,7 +160,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, if (tok->varId() > 0) { const std::map::const_iterator var = alloctype.find(tok->varId()); if (var != alloctype.end()) { - if (var->second.status == VarInfo::DEALLOC && (!Token::Match(tok, "%name% =") || tok->strAt(-1) == "*")) { + if (var->second.status == VarInfo::DEALLOC && tok->strAt(-1) != "&" && (!Token::Match(tok, "%name% =") || tok->strAt(-1) == "*")) { deallocUseError(tok, tok->str()); } else if (Token::simpleMatch(tok->tokAt(-2), "= &")) { varInfo->erase(tok->varId()); diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index c0c4adf37..4089baa7a 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -314,7 +314,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void deallocuse7() { // #6467, #6469, #6473 + void deallocuse7() { // #6467, #6469, #6473, #6648 check("struct Foo { int* ptr; };\n" "void f(Foo* foo) {\n" " delete foo->ptr;\n" @@ -349,6 +349,13 @@ private: " foo->ptr->func();\n" "}", true); ASSERT_EQUALS("", errout.str()); + + check("void foo(void (*conv)(char**)) {\n" + " char * ptr=(char*)malloc(42);\n" + " free(ptr);\n" + " (*conv)(&ptr);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void doublefree1() { // #3895