Skip to content

Commit

Permalink
Fix #8235 false positive: memory leak (realloc) (#6394)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed May 11, 2024
1 parent 8e7f060 commit 8842555
Show file tree
Hide file tree
Showing 2 changed files with 16 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 @@ -401,7 +401,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
if (Token::simpleMatch(tokAssignOp->astOperand1(), "."))
continue;
// taking address of another variable..
if (Token::Match(tokAssignOp, "= %var% [+;]")) {
if (Token::Match(tokAssignOp, "= %var% +|;|?|%comp%")) {
if (varTok->tokAt(2)->varId() != varTok->varId()) {
// If variable points at allocated memory => error
leakIfAllocated(varTok, varInfo);
Expand Down
15 changes: 15 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class TestLeakAutoVar : public TestFixture {
TEST_CASE(assign23);
TEST_CASE(assign24); // #7440
TEST_CASE(assign25);
TEST_CASE(assign26);

TEST_CASE(isAutoDealloc);

Expand Down Expand Up @@ -620,6 +621,20 @@ class TestLeakAutoVar : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void assign26() {
check("void f(int*& x) {\n" // #8235
" int* p = (int*)malloc(10);\n"
" x = p ? p : nullptr;\n"
"}", true);
ASSERT_EQUALS("", errout_str());

check("void f(int*& x) {\n"
" int* p = (int*)malloc(10);\n"
" x = p != nullptr ? p : nullptr;\n"
"}", true);
ASSERT_EQUALS("", errout_str());
}

void isAutoDealloc() {
check("void f() {\n"
" char *p = new char[100];"
Expand Down

0 comments on commit 8842555

Please sign in to comment.