From 86763b7b0aaac78206dbfbf0ccba40bb040ac4ac Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 26 May 2022 00:03:30 +0200 Subject: [PATCH] Fix FP memleak with array (#4133) * Fix #11019 FN memleak with redundant pointer op * Style * Fix #7705 FN: Memory leak not detected on struct member * Fix FP memleak with function call, fix cppcheckError * Fix FP memleak with array --- lib/checkmemoryleak.cpp | 2 +- test/testmemleak.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 240dd82f6..0ae71282e 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -795,7 +795,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var return {}; const Token* top = varTok; while (top->astParent()) { - if (top->astParent()->str() == "(") + if (Token::Match(top->astParent(), "(|[")) return {}; top = top->astParent(); } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index b5ea3cb6d..7dd1492c8 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1922,6 +1922,14 @@ private: " sess->fp = popen(cmd, rcmd == RSH_PIPE_READ ? \"r\" : \"w\");\n" "}\n", false); ASSERT_EQUALS("", errout.str()); + + check("struct S { char* a[2]; };\n" + "enum E { E0, E1 };\n" + "void f(struct S* s, enum E e, const char* n) {\n" + " free(s->a[e]);\n" + " s->a[e] = strdup(n);\n" + "}\n", false); + ASSERT_EQUALS("", errout.str()); } void failedAllocation() {