Skip to content

Commit

Permalink
Fix #12186 FN memleak with outparam allocation (#5661)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Nov 16, 2023
1 parent 9cf0342 commit 63a5a71
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
13 changes: 10 additions & 3 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,17 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
if (!isnull && (!af || af->arg == argNr)) {
const Library::AllocFunc* deallocFunc = mSettings->library.getDeallocFuncInfo(tokName);
VarInfo::AllocInfo dealloc(deallocFunc ? deallocFunc->groupId : 0, VarInfo::DEALLOC, tokName);
if (dealloc.type == 0)
changeAllocStatus(varInfo, allocation, tokName, arg);
if (const Library::AllocFunc* allocFunc = mSettings->library.getAllocFuncInfo(tokName)) {
if (allocFunc->arg == argNr) {
leakIfAllocated(arg, varInfo);
VarInfo::AllocInfo& varAlloc = varInfo.alloctype[arg->varId()];
varAlloc.type = allocFunc->groupId;
varAlloc.status = VarInfo::ALLOC;
varAlloc.allocTok = arg;
}
}
else
changeAllocStatus(varInfo, dealloc, tokName, arg);
changeAllocStatus(varInfo, dealloc.type == 0 ? allocation : dealloc, tokName, arg);
}
}
// Check smart pointer
Expand Down
19 changes: 16 additions & 3 deletions test/cfg/gnu.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,22 @@ void memleak_asprintf(char **ptr, const char *fmt, const int arg)
if (-1 != asprintf(ptr,fmt,arg)) {
free(ptr);
}
if (-1 != asprintf(ptr,fmt,arg)) {
// TODO: Related to #8980 cppcheck-suppress memleak
}
if (-1 != asprintf(ptr,fmt,arg)) {}
// cppcheck-suppress memleak
}

void memleak_asprintf2() { // #12186
char* p = malloc(5);
// cppcheck-suppress memleak
(void)asprintf(&p, "%s", "test");
// cppcheck-suppress memleak
}

void memleak_asprintf3() {
char* p = malloc(5);
// cppcheck-suppress memleak
asprintf(&p, "%s", "test");
free(p);
}

void memleak_xmalloc()
Expand Down
2 changes: 1 addition & 1 deletion test/cfg/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void resourceLeak_sqlite3_open()
sqlite3 * db;

sqlite3_open("/db", &db);
// TODO: cppcheck-suppress resourceLeak
// cppcheck-suppress resourceLeak
}

void ignoredReturnValue(const char * buf)
Expand Down
9 changes: 4 additions & 5 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3014,18 +3014,17 @@ class TestLeakAutoVarWindows : public TestFixture {
" HeapFree(MyHeap, 0, a);"
" HeapFree(MyHeap, 0, b);"
"}");
TODO_ASSERT_EQUALS("[test.c:1] (error) Resource leak: MyHeap",
"", errout.str());
ASSERT_EQUALS("[test.c:1]: (error) Resource leak: MyHeap\n", errout.str());

check("void f() {"
" HANDLE MyHeap = HeapCreate(0, 0, 0);"
" int *a = HeapAlloc(MyHeap, 0, sizeof(int));"
" int *b = HeapAlloc(MyHeap, 0, sizeof(int));"
" HeapFree(MyHeap, 0, a);"
"}");
TODO_ASSERT_EQUALS("[test.c:1] (error) Memory leak: MyHeap\n"
"[test.c:1] (error) Memory leak: b",
"[test.c:1]: (error) Memory leak: b\n", errout.str());
ASSERT_EQUALS("[test.c:1]: (error) Resource leak: MyHeap\n"
"[test.c:1]: (error) Memory leak: b\n",
errout.str());
}
};

Expand Down

0 comments on commit 63a5a71

Please sign in to comment.