From 501ab0b6dac636caff29e189df5f2b2bbf3d9ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 11 Aug 2015 12:41:42 +0200 Subject: [PATCH] CheckLeakAutoVar: updated handling of unknown types in C++ --- lib/checkleakautovar.cpp | 16 ++++++++++------ test/testleakautovar.cpp | 5 ++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index bc9b8f0b9..eddd7adea 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -235,18 +235,22 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, // not a local variable nor argument? const Variable *var = tok->variable(); - if (var && !var->isArgument() && (!var->isLocal() || var->isStatic())) { - continue; - } - - // non-pod variable - if (_tokenizer->isCPP() && (!var || !var->typeStartToken()->isStandardType())) + if (var && !var->isArgument() && (!var->isLocal() || var->isStatic())) continue; // Don't check reference variables if (var && var->isReference()) continue; + // non-pod variable + if (_tokenizer->isCPP()) { + if (!var) + continue; + // Possibly automatically deallocated memory + if (!var->typeStartToken()->isStandardType() && Token::Match(tok, "%var% = new")) + continue; + } + // allocation? if (tok->next()->astOperand2() && Token::Match(tok->next()->astOperand2()->previous(), "%type% (")) { int i = _settings->library.alloc(tok->next()->astOperand2()->previous()); diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 5dabaf6a3..e77dca449 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -1068,10 +1068,13 @@ private: void test5() { // unknown type check("void f() { Fred *p = malloc(10); }", true); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:1]: (error) Memory leak: p\n", errout.str()); check("void f() { Fred *p = malloc(10); }", false); ASSERT_EQUALS("[test.c:1]: (error) Memory leak: p\n", errout.str()); + + check("void f() { Fred *p = new Fred; }", true); + ASSERT_EQUALS("", errout.str()); } void throw1() { // 3987 - Execution reach a 'throw'