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 #10905, #11665 FN deallocuse #5751

Merged
merged 11 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
12 changes: 8 additions & 4 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
const Token * closingParenthesis = tok->linkAt(1);
for (const Token *innerTok = tok->tokAt(2); innerTok && innerTok != closingParenthesis; innerTok = innerTok->next()) {
// TODO: replace with checkTokenInsideExpression()
const Token* const openingPar = isFunctionCall(innerTok);
if (!openingPar)
checkTokenInsideExpression(innerTok, varInfo);

if (!isLocalVarNoAutoDealloc(innerTok, mTokenizer->isCPP()))
continue;
Expand Down Expand Up @@ -502,7 +505,6 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
}

// check for function call
const Token * const openingPar = isFunctionCall(innerTok);
if (openingPar) {
const Library::AllocFunc* allocFunc = mSettings->library.getDeallocFuncInfo(innerTok);
// innerTok is a function name
Expand Down Expand Up @@ -805,7 +807,8 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
const Token * vtok = typeEndTok->tokAt(3);
const VarInfo::AllocInfo allocation(af ? af->groupId : (arrayDelete ? NEW_ARRAY : NEW), VarInfo::OWNED, ftok);
changeAllocStatus(varInfo, allocation, vtok, vtok);
}
} else if (Token::Match(tok, "%var% ."))
checkTokenInsideExpression(tok, varInfo);
}
ret(endToken, varInfo, true);
return true;
Expand Down Expand Up @@ -930,8 +933,7 @@ void CheckLeakAutoVar::changeAllocStatus(VarInfo &varInfo, const VarInfo::AllocI
void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpeningPar, VarInfo &varInfo, const VarInfo::AllocInfo& allocation, const Library::AllocFunc* af)
{
// Ignore function call?
if (mSettings->library.isLeakIgnore(mSettings->library.getFunctionName(tokName)))
return;
const bool isLeakIgnore = mSettings->library.isLeakIgnore(mSettings->library.getFunctionName(tokName));
if (mSettings->library.getReallocFuncInfo(tokName))
return;

Expand Down Expand Up @@ -990,6 +992,8 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin
varAlloc.allocTok = arg;
}
}
else if (isLeakIgnore)
checkTokenInsideExpression(arg, varInfo);
else
changeAllocStatus(varInfo, dealloc.type == 0 ? allocation : dealloc, tokName, arg);
}
Expand Down
2 changes: 1 addition & 1 deletion test/cfg/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ void memleak_scandir(void)
which is allocated via malloc(3). If filter is NULL, all entries are
selected.*/

// TODO: cppcheck-suppress memleak
// cppcheck-suppress memleak
}

void no_memleak_scandir(void)
Expand Down
10 changes: 10 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5194,6 +5194,16 @@ class TestCondition : public TestFixture {
" for (int i = 0; i < N; a[i++] = false);\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f() {\n" // #8192
" for (int i = 0; i > 10; ++i) {}\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Condition 'i>10' is always false\n", "", errout.str());

check("void f() {\n"
" for (int i = 1000; i < 20; ++i) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Condition 'i<20' is always false\n", errout.str());
}

void alwaysTrueTryCatch()
Expand Down
29 changes: 28 additions & 1 deletion test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class TestLeakAutoVar : public TestFixture {
TEST_CASE(deallocuse11); // #8302
TEST_CASE(deallocuse12);
TEST_CASE(deallocuse13);
TEST_CASE(deallocuse14);

TEST_CASE(doublefree1);
TEST_CASE(doublefree2);
Expand Down Expand Up @@ -924,6 +925,25 @@ class TestLeakAutoVar : public TestFixture {
errout.str());
}

void deallocuse14() {
check("struct S { void f(); };\n" // #10905
"void g() {\n"
" S* s = new S;\n"
" delete s;\n"
" s->f();\n"
"}\n", true);
ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 's' after it is deallocated / released\n",
errout.str());

check("void f() {\n"
" int *p = (int*)malloc(4);\n"
" free(p);\n"
" if (*p == 5) {}\n"
"}\n");
ASSERT_EQUALS("[test.c:4]: (error) Dereferencing 'p' after it is deallocated / released\n",
errout.str());
}

void doublefree1() { // #3895
check("void f(char *p) {\n"
" if (x)\n"
Expand Down Expand Up @@ -2996,7 +3016,14 @@ class TestLeakAutoVarStrcpy : public TestFixture {
" free(p);\n"
" strcpy(a, p);\n"
"}");
TODO_ASSERT_EQUALS("error (free,use)", "", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (error) Dereferencing 'p' after it is deallocated / released\n", errout.str());

check("void f(char *p, const char *q) {\n" // #11665
" free(p);\n"
" strcpy(p, q);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Dereferencing 'p' after it is deallocated / released\n",
errout.str());

check("void f(char *p) {\n" // #3041 - assigning pointer when it's used
" free(p);\n"
Expand Down
Loading