Fix #10857 FN: leakNoVarFunctionCall (#4236)

* Fix #10857 FN: leakNoVarFunctionCall

* Fix TODO
This commit is contained in:
chrchr-github 2022-06-27 14:19:19 +02:00 committed by GitHub
parent 242afc389d
commit 88bf11abba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -1016,8 +1016,10 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope)
for (const Token* arg : args) {
if (arg->isOp())
continue;
while (arg->astOperand1())
arg = arg->astOperand1();
if (!(mTokenizer->isCPP() && Token::simpleMatch(arg, "new"))) {
while (arg->astOperand1())
arg = arg->astOperand1();
}
if (getAllocationType(arg, 0) == No)
continue;
if (isReopenStandardStream(arg))
@ -1068,8 +1070,9 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
if (closingBrace->str() == "}" && Token::Match(closingBrace->link()->tokAt(-1), "%name%") && (!isNew && precedes(tok, closingBrace->link())))
continue;
returnValueNotUsedError(tok, tok->str());
} else if (Token::Match(parent, "%comp%|!")) {
returnValueNotUsedError(tok, tok->str());
} else if (Token::Match(parent, "%comp%|!|,")) {
if (!(parent->astParent() && parent->str() == ","))
returnValueNotUsedError(tok, tok->str());
}
}
}

View File

@ -2401,6 +2401,12 @@ private:
" return static_cast<int*>(malloc(size));\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() { if (new int[42]) {} }\n" // #10857
"void g() { if (malloc(42)) {} }\n");
ASSERT_EQUALS("[test.cpp:1]: (error) Allocation with new, if doesn't release it.\n"
"[test.cpp:2]: (error) Allocation with malloc, if doesn't release it.\n",
errout.str());
}
void missingAssignment() {
@ -2452,7 +2458,7 @@ private:
"{\n"
" 42,malloc(42);\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'malloc' is not stored.\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'malloc' is not stored.\n", errout.str());
check("void *f()\n"
"{\n"