Fix #10186 (FP memleak with cast and assignment) (#3148)

This commit is contained in:
Rikard Falkeborn 2021-02-25 10:55:34 +01:00 committed by GitHub
parent 0988448319
commit 951ca2c8b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -764,6 +764,9 @@ const Token * CheckLeakAutoVar::checkTokenInsideExpression(const Token * const t
break;
rhs = rhs->astParent();
}
while (rhs->isCast()) {
rhs = rhs->astOperand1();
}
if (rhs->varId() == tok->varId()) {
// simple assignment
varInfo->erase(tok->varId());

View File

@ -80,6 +80,7 @@ private:
TEST_CASE(assign18);
TEST_CASE(assign19);
TEST_CASE(assign20); // #9187
TEST_CASE(assign21); // #10186
TEST_CASE(isAutoDealloc);
@ -421,6 +422,20 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout.str());
}
void assign21() { // #10186
check("void f(int **x) {\n"
" void *p = malloc(10);\n"
" *x = p;\n"
"}", true);
ASSERT_EQUALS("", errout.str());
check("void f(struct str *d) {\n"
" void *p = malloc(10);\n"
" d->a = p;\n"
"}", true);
ASSERT_EQUALS("", errout.str());
}
void isAutoDealloc() {
check("void f() {\n"
" char *p = new char[100];"