Fix FP memleak with function call, fix cppcheckError (#4130)

* Fix #11019 FN memleak with redundant pointer op

* Style

* Fix #7705 FN: Memory leak not detected on struct member

* Fix FP memleak with function call, fix cppcheckError
This commit is contained in:
chrchr-github 2022-05-24 14:02:06 +02:00 committed by GitHub
parent 2ceaf308de
commit 1ed280fe63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -793,8 +793,13 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var
auto isMemberAssignment = [](const Token* varTok, int varId) -> std::pair<const Token*, const Token*> {
if (varTok->varId() != varId)
return {};
const Token* top = varTok->astTop();
if (!Token::simpleMatch(top, "="))
const Token* top = varTok;
while (top->astParent()) {
if (top->astParent()->str() == "(")
return {};
top = top->astParent();
}
if (!Token::simpleMatch(top, "=") || !precedes(varTok, top))
return {};
const Token* dot = top->astOperand1();
while (dot && dot->str() != ".")
@ -824,7 +829,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var
break;
// Struct member is allocated => check if it is also properly deallocated..
else if ((assignToks = isMemberAssignment(tok2, variable->declarationId())).first) {
else if ((assignToks = isMemberAssignment(tok2, variable->declarationId())).first && assignToks.first->varId()) {
if (getAllocationType(assignToks.second, assignToks.first->varId()) == AllocType::No)
continue;

View File

@ -1909,6 +1909,19 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: s.p\n"
"[test.cpp:5]: (error) Memory leak: s.q\n",
errout.str());
check("struct S** f(struct S** s) {\n" // don't throw
" struct S** ret = malloc(sizeof(*ret));\n"
" ret[0] = malloc(sizeof(**s));\n"
" ret[0]->g = strdup(s[0]->g);\n"
" return ret;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void run_rcmd(enum rcommand rcmd, rsh_session *sess, char *cmd) {\n"
" sess->fp = popen(cmd, rcmd == RSH_PIPE_READ ? \"r\" : \"w\");\n"
"}\n", false);
ASSERT_EQUALS("", errout.str());
}
void failedAllocation() {