Fix #12289 FN (regression): memory leak not shown when strcpy is used (#5809)

This commit is contained in:
chrchr-github 2023-12-24 14:42:07 +01:00 committed by GitHub
parent e553940e23
commit a7baf88d4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -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")) {

View File

@ -2999,6 +2999,7 @@ private:
TEST_CASE(returnedValue); // #9298
TEST_CASE(deallocuse2);
TEST_CASE(fclose_false_positive); // #9575
TEST_CASE(strcpy_false_negative);
}
void returnedValue() { // #9298
@ -3037,6 +3038,13 @@ private:
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)