From d565cde815a0368d2db008521207a34c176f34e1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 29 Nov 2021 22:51:35 +0100 Subject: [PATCH] Fix and tests for #7622, #10381, #10382 (#3588) * Add test cases for #10381, #10382 * Fix #7622 * Format --- lib/checkleakautovar.cpp | 15 +++++++++++---- test/testleakautovar.cpp | 6 ++++++ test/testmemleak.cpp | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 3a59f4ae8..1169e1954 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -859,10 +859,17 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin int argNr = 1; for (const Token *funcArg = tokFirstArg; funcArg; funcArg = funcArg->nextArgument()) { const Token* arg = funcArg; - if (mTokenizer->isCPP() && arg->str() == "new") { - arg = arg->next(); - if (Token::simpleMatch(arg, "( std :: nothrow )")) - arg = arg->tokAt(5); + if (mTokenizer->isCPP()) { + int tokAdvance = 0; + if (arg->str() == "new") + tokAdvance = 1; + else if (Token::simpleMatch(arg, "* new")) + tokAdvance = 2; + if (tokAdvance > 0) { + arg = arg->tokAt(tokAdvance); + if (Token::simpleMatch(arg, "( std :: nothrow )")) + arg = arg->tokAt(5); + } } // Skip casts diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index a92d7afe0..f068cf09f 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -471,6 +471,12 @@ private: " TestType *tt = new TestType();\n" "}", true); ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: tt\n", errout.str()); + + check("void f(Bar& b) {\n" // #7622 + " char* data = new char[10];\n" + " b = Bar(*new Foo(data));\n" + "}", /*cpp*/ true); + ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function Foo() should have / configuration\n", errout.str()); } void realloc1() { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 793cd7a14..8715951a2 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1685,6 +1685,7 @@ private: TEST_CASE(function2); // #2848: Taking address in function TEST_CASE(function3); // #3024: kernel list TEST_CASE(function4); // #3038: Deallocating in function + TEST_CASE(function5); // #10381, #10382 // Handle if-else TEST_CASE(ifelse); @@ -1920,6 +1921,22 @@ private: ASSERT_EQUALS("", errout.str()); } + void function5() { + check("struct s f() {\n" // #10381 + " struct s s1;\n" + " s1->x = malloc(1);\n" + " return (s1);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("struct nc_rpc nc_rpc_getconfig() {\n" // #13082 + " struct nc_rpc rpc;\n" + " rpc->filter = malloc(1);\n" + " return (nc_rpc)rpc;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void ifelse() { check("static void foo()\n" "{\n"