Skip to content

Commit a7baf88

Browse files
Fix #12289 FN (regression): memory leak not shown when strcpy is used (#5809)
1 parent e553940 commit a7baf88

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/checkleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ const Token * CheckLeakAutoVar::checkTokenInsideExpression(const Token * const t
841841
if (rhs->varId() == tok->varId()) {
842842
// simple assignment
843843
varInfo.erase(tok->varId());
844-
} else if (rhs->str() == "(" && !mSettings->library.returnValue(rhs->astOperand1()).empty()) {
844+
} else if (rhs->astParent() && rhs->str() == "(" && !mSettings->library.returnValue(rhs->astOperand1()).empty()) {
845845
// #9298, assignment through return value of a function
846846
const std::string &returnValue = mSettings->library.returnValue(rhs->astOperand1());
847847
if (startsWith(returnValue, "arg")) {

test/testleakautovar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,7 @@ class TestLeakAutoVarStrcpy : public TestFixture {
29992999
TEST_CASE(returnedValue); // #9298
30003000
TEST_CASE(deallocuse2);
30013001
TEST_CASE(fclose_false_positive); // #9575
3002+
TEST_CASE(strcpy_false_negative);
30023003
}
30033004

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

3041+
void strcpy_false_negative() { // #12289
3042+
check("void f() {\n"
3043+
" char* buf = new char[12];\n"
3044+
" strcpy(buf, \"123\");\n"
3045+
"}\n");
3046+
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: buf\n", errout.str());
3047+
}
30403048
};
30413049

30423050
REGISTER_TEST(TestLeakAutoVarStrcpy)

0 commit comments

Comments
 (0)