Fix and tests for #7622, #10381, #10382 (#3588)

* Add test cases for #10381, #10382

* Fix #7622

* Format
This commit is contained in:
chrchr-github 2021-11-29 22:51:35 +01:00 committed by GitHub
parent 396a69a630
commit d565cde815
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 4 deletions

View File

@ -859,11 +859,18 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
int argNr = 1; int argNr = 1;
for (const Token *funcArg = tokFirstArg; funcArg; funcArg = funcArg->nextArgument()) { for (const Token *funcArg = tokFirstArg; funcArg; funcArg = funcArg->nextArgument()) {
const Token* arg = funcArg; const Token* arg = funcArg;
if (mTokenizer->isCPP() && arg->str() == "new") { if (mTokenizer->isCPP()) {
arg = arg->next(); 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 )")) if (Token::simpleMatch(arg, "( std :: nothrow )"))
arg = arg->tokAt(5); arg = arg->tokAt(5);
} }
}
// Skip casts // Skip casts
while (arg && arg->isCast()) while (arg && arg->isCast())

View File

@ -471,6 +471,12 @@ private:
" TestType *tt = new TestType();\n" " TestType *tt = new TestType();\n"
"}", true); "}", true);
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: tt\n", errout.str()); 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 <use>/<leak-ignore> configuration\n", errout.str());
} }
void realloc1() { void realloc1() {

View File

@ -1685,6 +1685,7 @@ private:
TEST_CASE(function2); // #2848: Taking address in function TEST_CASE(function2); // #2848: Taking address in function
TEST_CASE(function3); // #3024: kernel list TEST_CASE(function3); // #3024: kernel list
TEST_CASE(function4); // #3038: Deallocating in function TEST_CASE(function4); // #3038: Deallocating in function
TEST_CASE(function5); // #10381, #10382
// Handle if-else // Handle if-else
TEST_CASE(ifelse); TEST_CASE(ifelse);
@ -1920,6 +1921,22 @@ private:
ASSERT_EQUALS("", errout.str()); 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() { void ifelse() {
check("static void foo()\n" check("static void foo()\n"
"{\n" "{\n"