Skip to content

Commit f80effe

Browse files
Fix #10996 FN memleak suppressed for library function by (void) (#5891)
1 parent 6c0bb5e commit f80effe

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

lib/checkleakautovar.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,15 +833,18 @@ const Token * CheckLeakAutoVar::checkTokenInsideExpression(const Token * const t
833833
} else {
834834
// check if tok is assigned into another variable
835835
const Token *rhs = tok;
836+
bool isAssignment = false;
836837
while (rhs->astParent()) {
837-
if (rhs->astParent()->str() == "=")
838+
if (rhs->astParent()->str() == "=") {
839+
isAssignment = true;
838840
break;
841+
}
839842
rhs = rhs->astParent();
840843
}
841844
while (rhs->isCast()) {
842845
rhs = rhs->astOperand2() ? rhs->astOperand2() : rhs->astOperand1();
843846
}
844-
if (rhs->varId() == tok->varId()) {
847+
if (rhs->varId() == tok->varId() && isAssignment) {
845848
// simple assignment
846849
varInfo.erase(tok->varId());
847850
} else if (rhs->astParent() && rhs->str() == "(" && !mSettings->library.returnValue(rhs->astOperand1()).empty()) {

test/testleakautovar.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,19 @@ class TestLeakAutoVar : public TestFixture {
455455
ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout.str());
456456
}
457457

458-
void assign21() { // #10186
459-
check("void f(int **x) {\n"
458+
void assign21() {
459+
check("void f(int **x) {\n" // #10186
460460
" void *p = malloc(10);\n"
461461
" *x = (int*)p;\n"
462462
"}", true);
463463
ASSERT_EQUALS("", errout.str());
464+
465+
check("struct S { int i; };\n" // #10996
466+
"void f() {\n"
467+
" S* s = new S();\n"
468+
" (void)s;\n"
469+
"}", true);
470+
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: s\n", errout.str());
464471
}
465472

466473
void assign22() { // #9139

0 commit comments

Comments
 (0)