From 9b79f3bd94f414af41303faf27afe91a8072848a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:27:58 +0200 Subject: [PATCH] Fix #12996 FP memleak after checking for failure (#6671) --- lib/checkleakautovar.cpp | 2 +- test/cfg/posix.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index f9465163097..5007d923b33 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -1153,7 +1153,7 @@ void CheckLeakAutoVar::ret(const Token *tok, VarInfo &varInfo, const bool isEndO const Token* const ifStart = ifEnd->link(); const Token* const alloc = it->second.allocTok; if (precedes(ifStart, alloc) && succeeds(ifEnd, alloc)) { // allocation and check in if - if (Token::Match(outparamFunc->next()->astParent(), "%comp%")) + if (outparamFunc->next()->astParent() == ifStart || Token::Match(outparamFunc->next()->astParent(), "%comp%")) continue; } else { // allocation result assigned to variable const Token* const retAssign = outparamFunc->next()->astParent(); diff --git a/test/cfg/posix.c b/test/cfg/posix.c index f135ea7d6de..69da6db2b2b 100644 --- a/test/cfg/posix.c +++ b/test/cfg/posix.c @@ -1057,6 +1057,15 @@ void memleak_getaddrinfo_if() // #12506 } } +void memleak_getaddrinfo_if2() // #12996 +{ + struct addrinfo *addrs = NULL; + if (getaddrinfo("a", "b", NULL, &addrs)) { + return; + } + freeaddrinfo(addrs); +} + void memleak_mmap(int fd) { // cppcheck-suppress [unusedAllocatedMemory, unreadVariable, constVariablePointer]