From 057f18798b9c09f263f40a63874520a4efab5de5 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 22 Jun 2023 17:01:52 +0200 Subject: [PATCH] Fix #11786 False positive: memory leak --- lib/checkleakautovar.cpp | 2 +- test/testleakautovar.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 99c819a4a33..dd68988b564 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -609,7 +609,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken, // unknown control.. (TODO: handle loops) else if ((Token::Match(tok, "%type% (") && Token::simpleMatch(tok->linkAt(1), ") {")) || Token::simpleMatch(tok, "do {")) { varInfo.clear(); - break; + return false; } // return diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 1ddcf20bf88..a11f1d50b2f 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -178,6 +178,7 @@ class TestLeakAutoVar : public TestFixture { // loops TEST_CASE(loop1); + TEST_CASE(loop2); // mismatching allocation/deallocation TEST_CASE(mismatchAllocDealloc); @@ -2063,6 +2064,17 @@ class TestLeakAutoVar : public TestFixture { ASSERT_EQUALS("", errout.str()); } + void loop2() { + check("void f() {\n" // #11786 + " int* p = (int*)malloc(sizeof(int));\n" + " if (1) {\n" + " while (0) {}\n" + " free(p);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void mismatchAllocDealloc() { check("void f() {\n" " FILE*f=fopen(fname,a);\n"