Skip to content

Commit fe56b0c

Browse files
LeakAutoVar: Handle C++ casts in function calls (cppcheck-opensource#5181)
1 parent 353f540 commit fe56b0c

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

lib/checkleakautovar.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,8 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
934934
}
935935

936936
// Skip casts
937+
if (arg->isKeyword() && arg->astParent() && arg->astParent()->isCast())
938+
arg = arg->astParent();
937939
while (arg && arg->isCast())
938940
arg = arg->astOperand2() ? arg->astOperand2() : arg->astOperand1();
939941
const Token * const argTypeStartTok = arg;

test/testleakautovar.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,13 @@ class TestLeakAutoVar : public TestFixture {
564564
" g();\n"
565565
"}\n", /*cpp*/ true);
566566
ASSERT_EQUALS("", errout.str());
567+
568+
check("void g() {}\n" // #10517
569+
"void f() {\n"
570+
" char* p = malloc(10);\n"
571+
" g();\n"
572+
"}\n");
573+
ASSERT_EQUALS("[test.c:5]: (error) Memory leak: p\n", errout.str());
567574
}
568575

569576
void isAutoDealloc() {
@@ -2751,6 +2758,20 @@ class TestLeakAutoVar : public TestFixture {
27512758
" free_func((void *)(1), buf);\n"
27522759
"}", settingsFunctionCall);
27532760
ASSERT_EQUALS("[test.cpp:5]: (information) --check-library: Function free_func() should have <use>/<leak-ignore> configuration\n", errout.str());
2761+
2762+
check("void g(void*);\n"
2763+
"void h(int, void*);\n"
2764+
"void f1() {\n"
2765+
" int* p = new int;\n"
2766+
" g(static_cast<void*>(p));\n"
2767+
"}\n"
2768+
"void f2() {\n"
2769+
" int* p = new int;\n"
2770+
" h(1, static_cast<void*>(p));\n"
2771+
"}\n", /*cpp*/ true);
2772+
ASSERT_EQUALS("[test.cpp:6]: (information) --check-library: Function g() should have <use>/<leak-ignore> configuration\n"
2773+
"[test.cpp:10]: (information) --check-library: Function h() should have <use>/<leak-ignore> configuration\n",
2774+
errout.str());
27542775
}
27552776

27562777
void functionCallLeakIgnoreConfig() { // #7923

0 commit comments

Comments
 (0)