From 65297ce285e9a231fe19a5b4a2b9a09b2cd51dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 4 May 2017 19:39:57 +0200 Subject: [PATCH] Fixed #7191 (false positive memleak on in-place new) --- lib/checkleakautovar.cpp | 2 +- test/testleakautovar.cpp | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index cc02044e8..b3d087e88 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -290,7 +290,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, alloctype[varTok->varId()].type = f->groupId; alloctype[varTok->varId()].status = VarInfo::ALLOC; } - } else if (_tokenizer->isCPP() && varTok->strAt(2) == "new") { + } else if (_tokenizer->isCPP() && Token::Match(varTok->tokAt(2), "new !!(")) { const Token* tok2 = varTok->tokAt(2)->astOperand1(); bool arrayNew = (tok2 && (tok2->str() == "[" || (tok2->str() == "(" && tok2->astOperand1() && tok2->astOperand1()->str() == "["))); alloctype[varTok->varId()].type = arrayNew ? -2 : -1; diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index db0e232be..b1a1d0db1 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -1066,6 +1066,11 @@ private: " free(cPtr);\n" "}", true); ASSERT_EQUALS("[test.cpp:3]: (error) Mismatching allocation and deallocation: cPtr\n", errout.str()); + + check("void f() {\n" + " char *cPtr = new (buf) char[100];\n" + "}", true); + ASSERT_EQUALS("", errout.str()); } void return1() {