Skip to content

Commit

Permalink
Fix #12289 FN (regression): memory leak not shown when strcpy is used
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Dec 24, 2023
1 parent 17ee409 commit 8ce4e78
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ const Token * CheckLeakAutoVar::checkTokenInsideExpression(const Token * const t
if (rhs->varId() == tok->varId()) {
// simple assignment
varInfo.erase(tok->varId());
} else if (rhs->str() == "(" && !mSettings->library.returnValue(rhs->astOperand1()).empty()) {
} else if (rhs->astParent() && rhs->str() == "(" && !mSettings->library.returnValue(rhs->astOperand1()).empty()) {
// #9298, assignment through return value of a function
const std::string &returnValue = mSettings->library.returnValue(rhs->astOperand1());
if (startsWith(returnValue, "arg")) {
Expand Down
8 changes: 8 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2999,6 +2999,7 @@ class TestLeakAutoVarStrcpy : public TestFixture {
TEST_CASE(returnedValue); // #9298
TEST_CASE(deallocuse2);
TEST_CASE(fclose_false_positive); // #9575
TEST_CASE(strcpy_false_negative);
}

void returnedValue() { // #9298
Expand Down Expand Up @@ -3037,6 +3038,13 @@ class TestLeakAutoVarStrcpy : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void strcpy_false_negative() { // #12289
check("void f() {\n"
" char* buf = new char[12];\n"
" strcpy(buf, \"123\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: buf\n", errout.str());
}
};

REGISTER_TEST(TestLeakAutoVarStrcpy)
Expand Down

0 comments on commit 8ce4e78

Please sign in to comment.