Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12186 FN memleak with outparam allocation #5661

Merged
merged 6 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,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 @@ -3004,18 +3004,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
Loading