From 75e367c48bcfb919a677d09e8a2bc1ded30aa4fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 9 Jan 2019 19:50:41 +0100 Subject: [PATCH] Fixed #8340 (incorrect memleak errors) --- lib/checkleakautovar.cpp | 2 +- test/testleakautovar.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 572840690..8ae0b5ecb 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -364,7 +364,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, for (const Token *innerTok = tok->tokAt(2); innerTok && innerTok != closingParenthesis; innerTok = innerTok->next()) { // TODO: replace with checkTokenInsideExpression() - if (Token::Match(innerTok, "%var% =")) { + if (Token::Match(innerTok, "%var% =") && innerTok->astParent() == innerTok->next()) { // allocation? if (Token::Match(innerTok->tokAt(2), "%type% (")) { const Library::AllocFunc* f = mSettings->library.alloc(innerTok->tokAt(2)); diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 6e62fcb2f..e23ff248c 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -100,6 +100,7 @@ private: TEST_CASE(ifelse9); // #5273 - if (X(p==NULL, 0)) TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL)) TEST_CASE(ifelse11); // #8365 - if (NULL == (p = malloc(4))) + TEST_CASE(ifelse12); // #8340 - if ((*p = malloc(4)) == NULL) // switch TEST_CASE(switch1); @@ -1172,6 +1173,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void ifelse12() { // #8340 + check("void f(char **p) {\n" + " if ((*p = malloc(4)) == NULL)\n" + " return;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void switch1() { check("void f() {\n" " char *p = 0;\n"