Repository navigation
Fix #14990: FP memleak with function call through function pointer #8813
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
Open
aadanen
wants to merge
11
commits into
cppcheck-opensource:main
Choose a base branch
from
aadanen:fptr_leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−23
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4ad99b5
impl and tests
aadanen b06830e
formatting
aadanen cf346bd
formatting
aadanen 34a5ccd
sanitizer
aadanen 67b2271
make functionCall not segfault
aadanen db1d8ac
(int) is not lambda call
aadanen bd424dd
fixed infinite loops on certain inputs
aadanen 22a3ab2
fixed redundantNextPrevious
aadanen d94e917
clarified checks and fixed test
aadanen cd4f032
format
aadanen a0fa6fb
call to array element
aadanen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -267,6 +267,47 @@ static const Token * isFunctionCall(const Token * nameToken) | |||||||||||||
| return nullptr; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** checks if tok is part of the LHS of an anonymous function call: | ||||||||||||||
| * tok should be the first token in a unary expression that evaluates to a | ||||||||||||||
| * function pointer, or the name of a lambda or function | ||||||||||||||
| * (*func_ptr)(arg) | ||||||||||||||
| * or | ||||||||||||||
| * (lambda)(arg) | ||||||||||||||
| * or | ||||||||||||||
| * get_function()(arg) | ||||||||||||||
| * or | ||||||||||||||
| * arr[idx](arg) | ||||||||||||||
| * @param tok on the LHS of a function call | ||||||||||||||
| * @return opening parenthesis token or nullptr if not a function call | ||||||||||||||
| */ | ||||||||||||||
| static const Token *isAnonymousFunctionCall(const Token *tok) | ||||||||||||||
| { | ||||||||||||||
| if (!tok || tok->isStandardType()) | ||||||||||||||
| return nullptr; | ||||||||||||||
|
|
||||||||||||||
| auto isLparNotCast = [](const Token *lpar) -> bool { | ||||||||||||||
| return !lpar->isCast() && lpar->str() == "("; | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| // function pointer or lambda | ||||||||||||||
| if (isLparNotCast(tok->previous())) { | ||||||||||||||
| tok = tok->linkAt(-1)->next(); | ||||||||||||||
| } else if (tok->isName() && (isLparNotCast(tok->next()) || tok->strAt(1) == "[")) { | ||||||||||||||
| // call to result of a function | ||||||||||||||
| tok = tok->linkAt(1)->next(); | ||||||||||||||
|
Comment on lines
+296
to
+297
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets handle multidimension arrays somehow for example:
Suggested change
|
||||||||||||||
| } else { | ||||||||||||||
| return nullptr; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // < could be a less than, not a template operator | ||||||||||||||
| if (tok->link() && tok->str() == "<") | ||||||||||||||
| tok = tok->link()->next(); | ||||||||||||||
|
|
||||||||||||||
| if (isLparNotCast(tok)) | ||||||||||||||
| return tok; | ||||||||||||||
| return nullptr; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| static const Token* getOutparamAllocation(const Token* tok, const Library& library) | ||||||||||||||
| { | ||||||||||||||
| if (!tok) | ||||||||||||||
|
|
@@ -787,9 +828,22 @@ bool CheckLeakAutoVarImpl::checkScope(const Token * const startToken, | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // a regular function call can return an anonymous function | ||||||||||||||
| openingPar = isAnonymousFunctionCall(ftok); | ||||||||||||||
| if (openingPar) { | ||||||||||||||
| functionCall(nullptr, openingPar, varInfo, VarInfo::AllocInfo(0, VarInfo::NOALLOC), nullptr); | ||||||||||||||
| tok = openingPar->link(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| continue; | ||||||||||||||
|
|
||||||||||||||
| } | ||||||||||||||
| // top level call to an anonymous function | ||||||||||||||
|
aadanen marked this conversation as resolved.
Resolved
|
||||||||||||||
| else if (const Token *lpar = isAnonymousFunctionCall(tok)) { | ||||||||||||||
| functionCall(nullptr, lpar, varInfo, VarInfo::AllocInfo(0, VarInfo::NOALLOC), nullptr); | ||||||||||||||
| tok = lpar->link(); | ||||||||||||||
|
aadanen marked this conversation as resolved.
Resolved
|
||||||||||||||
|
|
||||||||||||||
| } | ||||||||||||||
| // goto => weird execution path | ||||||||||||||
| else if (tok->str() == "goto") { | ||||||||||||||
| varInfo.clear(); | ||||||||||||||
|
|
@@ -941,6 +995,7 @@ const Token * CheckLeakAutoVarImpl::checkTokenInsideExpression(const Token * con | |||||||||||||
|
|
||||||||||||||
| // check for function call | ||||||||||||||
| const Token * const openingPar = inFuncCall ? nullptr : isFunctionCall(tok); | ||||||||||||||
| const Token * const anonOpeningPar = isAnonymousFunctionCall(tok); | ||||||||||||||
| if (openingPar) { | ||||||||||||||
| const Library::AllocFunc* allocFunc = mSettings.library.getDeallocFuncInfo(tok); | ||||||||||||||
| VarInfo::AllocInfo alloc(allocFunc ? allocFunc->groupId : 0, VarInfo::DEALLOC, tok); | ||||||||||||||
|
|
@@ -951,7 +1006,15 @@ const Token * CheckLeakAutoVarImpl::checkTokenInsideExpression(const Token * con | |||||||||||||
| if (startsWith(returnValue, "arg")) | ||||||||||||||
| // the function returns one of its argument, we need to process a potential assignment | ||||||||||||||
| return openingPar; | ||||||||||||||
| return isCPPCast(tok->astParent()) ? openingPar : openingPar->link(); | ||||||||||||||
|
|
||||||||||||||
| if (!anonOpeningPar) | ||||||||||||||
| return isCPPCast(tok->astParent()) ? openingPar : openingPar->link(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // check for anonymous function call | ||||||||||||||
| if (anonOpeningPar) { | ||||||||||||||
| functionCall(nullptr, anonOpeningPar, varInfo, VarInfo::AllocInfo(0, VarInfo::NOALLOC), nullptr); | ||||||||||||||
| return anonOpeningPar->link(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return nullptr; | ||||||||||||||
|
|
@@ -1018,11 +1081,10 @@ void CheckLeakAutoVarImpl::changeAllocStatus(VarInfo &varInfo, const VarInfo::Al | |||||||||||||
|
|
||||||||||||||
| void CheckLeakAutoVarImpl::functionCall(const Token *tokName, const Token *tokOpeningPar, VarInfo &varInfo, const VarInfo::AllocInfo& allocation, const Library::AllocFunc* af) | ||||||||||||||
| { | ||||||||||||||
| // Ignore function call? | ||||||||||||||
| const bool isLeakIgnore = mSettings.library.isLeakIgnore(mSettings.library.getFunctionName(tokName)); | ||||||||||||||
| if (mSettings.library.getReallocFuncInfo(tokName)) | ||||||||||||||
| const bool isLeakIgnore = tokName ? mSettings.library.isLeakIgnore(mSettings.library.getFunctionName(tokName)) : false; | ||||||||||||||
| if (tokName && mSettings.library.getReallocFuncInfo(tokName)) | ||||||||||||||
| return; | ||||||||||||||
| if (tokName->next()->valueType() && tokName->next()->valueType()->container && tokName->next()->valueType()->container->stdStringLike) | ||||||||||||||
| if (tokName && tokName->next()->valueType() && tokName->next()->valueType()->container && tokName->next()->valueType()->container->stdStringLike) | ||||||||||||||
| return; | ||||||||||||||
|
|
||||||||||||||
| const Token * const tokFirstArg = tokOpeningPar->next(); | ||||||||||||||
|
|
@@ -1071,26 +1133,30 @@ void CheckLeakAutoVarImpl::functionCall(const Token *tokName, const Token *tokOp | |||||||||||||
|
|
||||||||||||||
| // Is variable allocated? | ||||||||||||||
| if (!isnull && (!af || af->arg == argNr)) { | ||||||||||||||
| const Library::AllocFunc* deallocFunc = mSettings.library.getDeallocFuncInfo(tokName); | ||||||||||||||
| const Library::AllocFunc* deallocFunc = tokName ? mSettings.library.getDeallocFuncInfo(tokName) : nullptr; | ||||||||||||||
| VarInfo::AllocInfo dealloc(deallocFunc ? deallocFunc->groupId : 0, VarInfo::DEALLOC, tokName); | ||||||||||||||
| if (const Library::AllocFunc* allocFunc = mSettings.library.getAllocFuncInfo(tokName)) { | ||||||||||||||
| if (mSettings.library.getDeallocFuncInfo(tokName)) { | ||||||||||||||
| changeAllocStatus(varInfo, dealloc.type == 0 ? allocation : dealloc, tokName, arg); | ||||||||||||||
| } | ||||||||||||||
| if (allocFunc->arg == argNr && | ||||||||||||||
| !(arg->variable() && arg->variable()->isArgument() && arg->valueType() && arg->valueType()->pointer > 1) && | ||||||||||||||
| (isAddressOf || (arg->valueType() && arg->valueType()->pointer == 2))) { | ||||||||||||||
| leakIfAllocated(arg, varInfo); | ||||||||||||||
| VarInfo::AllocInfo& varAlloc = varInfo.alloctype[arg->varId()]; | ||||||||||||||
| varAlloc.type = allocFunc->groupId; | ||||||||||||||
| varAlloc.status = VarInfo::ALLOC; | ||||||||||||||
| varAlloc.allocTok = arg; | ||||||||||||||
| if (tokName) { | ||||||||||||||
| if (const Library::AllocFunc* allocFunc = mSettings.library.getAllocFuncInfo(tokName)) { | ||||||||||||||
| if (mSettings.library.getDeallocFuncInfo(tokName)) { | ||||||||||||||
| changeAllocStatus(varInfo, dealloc.type == 0 ? allocation : dealloc, tokName, arg); | ||||||||||||||
| } | ||||||||||||||
| if (allocFunc->arg == argNr && | ||||||||||||||
| !(arg->variable() && arg->variable()->isArgument() && arg->valueType() && arg->valueType()->pointer > 1) && | ||||||||||||||
| (isAddressOf || (arg->valueType() && arg->valueType()->pointer == 2))) { | ||||||||||||||
| leakIfAllocated(arg, varInfo); | ||||||||||||||
| VarInfo::AllocInfo& varAlloc = varInfo.alloctype[arg->varId()]; | ||||||||||||||
| varAlloc.type = allocFunc->groupId; | ||||||||||||||
| varAlloc.status = VarInfo::ALLOC; | ||||||||||||||
| varAlloc.allocTok = arg; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| else if (isLeakIgnore) | ||||||||||||||
| checkTokenInsideExpression(arg, varInfo); | ||||||||||||||
| else | ||||||||||||||
| changeAllocStatus(varInfo, dealloc.type == 0 ? allocation : dealloc, tokName, arg); | ||||||||||||||
| } else { | ||||||||||||||
| changeAllocStatus(varInfo, allocation, nullptr, arg); | ||||||||||||||
| } | ||||||||||||||
| else if (isLeakIgnore) | ||||||||||||||
| checkTokenInsideExpression(arg, varInfo); | ||||||||||||||
| else | ||||||||||||||
| changeAllocStatus(varInfo, dealloc.type == 0 ? allocation : dealloc, tokName, arg); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| // Check smart pointer | ||||||||||||||
|
|
@@ -1269,7 +1335,7 @@ void CheckLeakAutoVarImpl::ret(const Token *tok, VarInfo &varInfo, const bool is | |||||||||||||
| const auto use = possibleUsage.find(varid); | ||||||||||||||
| if (use == possibleUsage.end()) { | ||||||||||||||
| leakError(tok, var->name(), it->second.type); | ||||||||||||||
| } else if (!use->second.first->variable()) { // TODO: handle constructors | ||||||||||||||
| } else if (use->second.first && !use->second.first->variable()) { // TODO: handle constructors | ||||||||||||||
| configurationInfo(tok, use->second); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.