From 96661628b992d3dfe21fa7695a2f10a9fc9517a1 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 22 Jun 2023 11:50:37 +0200 Subject: [PATCH 1/2] LeakAutoVar: Handle C++ casts in function calls --- lib/checkleakautovar.cpp | 2 ++ test/testleakautovar.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 99c819a4a33..f7eb0f33c1b 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -934,6 +934,8 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin } // Skip casts + if (arg->isKeyword() && arg->astParent() && arg->astParent()->isCast()) + arg = arg->astParent(); while (arg && arg->isCast()) arg = arg->astOperand2() ? arg->astOperand2() : arg->astOperand1(); const Token * const argTypeStartTok = arg; diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 1ddcf20bf88..8984bb74767 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -2751,6 +2751,20 @@ class TestLeakAutoVar : public TestFixture { " free_func((void *)(1), buf);\n" "}", settingsFunctionCall); ASSERT_EQUALS("[test.cpp:5]: (information) --check-library: Function free_func() should have / configuration\n", errout.str()); + + check("void g(void*);\n" + "void h(int, void*);\n" + "void f1() {\n" + " int* p = new int;\n" + " g(static_cast(p));\n" + "}\n" + "void f2() {\n" + " int* p = new int;\n" + " h(1, static_cast(p));\n" + "}\n", /*cpp*/ true); + ASSERT_EQUALS("[test.cpp:6]: (information) --check-library: Function g() should have / configuration\n" + "[test.cpp:10]: (information) --check-library: Function h() should have / configuration\n", + errout.str()); } void functionCallLeakIgnoreConfig() { // #7923 From 6cc4c52f54b01b784218fa4efab74ea523756535 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 22 Jun 2023 13:23:42 +0200 Subject: [PATCH 2/2] Add test for #10517 --- test/testleakautovar.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 8984bb74767..7df909f8bf5 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -564,6 +564,13 @@ class TestLeakAutoVar : public TestFixture { " g();\n" "}\n", /*cpp*/ true); ASSERT_EQUALS("", errout.str()); + + check("void g() {}\n" // #10517 + "void f() {\n" + " char* p = malloc(10);\n" + " g();\n" + "}\n"); + ASSERT_EQUALS("[test.c:5]: (error) Memory leak: p\n", errout.str()); } void isAutoDealloc() {