Skip to content

Commit 3c21b9c

Browse files
Fix #11038, FP memory leak in if-statement in realloc with cast (danmar#4572)
1 parent e4c5f36 commit 3c21b9c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/checkleakautovar.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -452,18 +452,18 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
452452
while (tokRightAstOperand && tokRightAstOperand->isCast())
453453
tokRightAstOperand = tokRightAstOperand->astOperand2() ? tokRightAstOperand->astOperand2() : tokRightAstOperand->astOperand1();
454454
if (tokRightAstOperand && Token::Match(tokRightAstOperand->previous(), "%type% (")) {
455-
const Library::AllocFunc* f = mSettings->library.getAllocFuncInfo(tokRightAstOperand->previous());
455+
const Token * fTok = tokRightAstOperand->previous();
456+
const Library::AllocFunc* f = mSettings->library.getAllocFuncInfo(fTok);
456457
if (f && f->arg == -1) {
457458
VarInfo::AllocInfo& varAlloc = alloctype[innerTok->varId()];
458459
varAlloc.type = f->groupId;
459460
varAlloc.status = VarInfo::ALLOC;
460-
varAlloc.allocTok = tokRightAstOperand->previous();
461+
varAlloc.allocTok = fTok;
461462
} else {
462463
// Fixme: warn about leak
463464
alloctype.erase(innerTok->varId());
464465
}
465-
466-
changeAllocStatusIfRealloc(alloctype, innerTok->tokAt(2), varTok);
466+
changeAllocStatusIfRealloc(alloctype, fTok, varTok);
467467
} else if (mTokenizer->isCPP() && Token::Match(innerTok->tokAt(2), "new !!(")) {
468468
const Token* tok2 = innerTok->tokAt(2)->astOperand1();
469469
const bool arrayNew = (tok2 && (tok2->str() == "[" || (tok2->str() == "(" && tok2->astOperand1() && tok2->astOperand1()->str() == "[")));

test/testleakautovar.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class TestLeakAutoVar : public TestFixture {
169169
TEST_CASE(ifelse25); // #9966
170170
TEST_CASE(ifelse26);
171171
TEST_CASE(ifelse27);
172+
TEST_CASE(ifelse28); // #11038
172173

173174
// switch
174175
TEST_CASE(switch1);
@@ -1909,6 +1910,17 @@ class TestLeakAutoVar : public TestFixture {
19091910
ASSERT_EQUALS("", errout.str());
19101911
}
19111912

1913+
void ifelse28() { // #11038
1914+
check("char * f(void) {\n"
1915+
" char *buf = (char*)malloc(42*sizeof(char));\n"
1916+
" char *temp;\n"
1917+
" if ((temp = (char*)realloc(buf, 16)) != NULL)\n"
1918+
" { buf = temp; }\n"
1919+
" return buf;\n"
1920+
"}\n");
1921+
ASSERT_EQUALS("", errout.str());
1922+
}
1923+
19121924
void switch1() {
19131925
check("void f() {\n"
19141926
" char *p = 0;\n"

0 commit comments

Comments
 (0)