Skip to content

Commit

Permalink
Don't report checkLibraryUseIgnore for known functions (refs #7719)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Nov 15, 2023
1 parent 282c195 commit a19142a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
18 changes: 10 additions & 8 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void VarInfo::print()
std::string strusage;
const auto use = possibleUsage.find(it->first);
if (use != possibleUsage.end())
strusage = use->second.first;
strusage = use->second.first->str();

std::string status;
switch (it->second.status) {
Expand Down Expand Up @@ -140,7 +140,7 @@ void VarInfo::print()
}
}

void VarInfo::possibleUsageAll(const std::pair<std::string, Usage>& functionUsage)
void VarInfo::possibleUsageAll(const std::pair<const Token*, Usage>& functionUsage)
{
possibleUsage.clear();
for (std::map<int, AllocInfo>::const_iterator it = alloctype.cbegin(); it != alloctype.cend(); ++it)
Expand Down Expand Up @@ -176,13 +176,15 @@ void CheckLeakAutoVar::deallocReturnError(const Token *tok, const Token *dealloc
reportError(locations, Severity::error, "deallocret", "$symbol:" + varname + "\nReturning/dereferencing '$symbol' after it is deallocated / released", CWE672, Certainty::normal);
}

void CheckLeakAutoVar::configurationInfo(const Token* tok, const std::pair<std::string, VarInfo::Usage>& functionUsage)
void CheckLeakAutoVar::configurationInfo(const Token* tok, const std::pair<const Token*, VarInfo::Usage>& functionUsage)
{
if (mSettings->checkLibrary && functionUsage.second == VarInfo::USED) {
if (mSettings->checkLibrary && functionUsage.second == VarInfo::USED &&
(!functionUsage.first || !functionUsage.first->function() || !functionUsage.first->function()->hasBody())) {
const std::string funcStr = functionUsage.first ? mSettings->library.getFunctionName(functionUsage.first) : "f";
reportError(tok,
Severity::information,
"checkLibraryUseIgnore",
"--check-library: Function " + functionUsage.first + "() should have <use>/<leak-ignore> configuration");
"--check-library: Function " + funcStr + "() should have <use>/<leak-ignore> configuration");
}
}

Expand Down Expand Up @@ -684,7 +686,6 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
if (allocation.status == VarInfo::NOALLOC && Token::simpleMatch(tok, ") ; }")) {
if (ftok->isKeyword())
continue;
const std::string functionName(mSettings->library.getFunctionName(ftok));
bool unknown = false;
if (mTokenizer->isScopeNoReturn(tok->tokAt(2), &unknown)) {
if (!unknown)
Expand All @@ -693,9 +694,10 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
if (ftok->function() && !ftok->function()->isAttributeNoreturn() &&
!(ftok->function()->functionScope && mTokenizer->isScopeNoReturn(ftok->function()->functionScope->bodyEnd))) // check function scope
continue;
const std::string functionName(mSettings->library.getFunctionName(ftok));
if (!mSettings->library.isLeakIgnore(functionName) && !mSettings->library.isUse(functionName)) {
const VarInfo::Usage usage = Token::simpleMatch(openingPar, "( )") ? VarInfo::NORET : VarInfo::USED; // TODO: check parameters passed to function
varInfo.possibleUsageAll({ functionName, usage });
varInfo.possibleUsageAll({ ftok, usage });
}
}
}
Expand Down Expand Up @@ -895,7 +897,7 @@ void CheckLeakAutoVar::changeAllocStatus(VarInfo &varInfo, const VarInfo::AllocI
if (var != alloctype.end()) {
if (allocation.status == VarInfo::NOALLOC) {
// possible usage
varInfo.possibleUsage[arg->varId()] = { tok->str(), VarInfo::USED };
varInfo.possibleUsage[arg->varId()] = { tok, VarInfo::USED };
if (var->second.status == VarInfo::DEALLOC && arg->previous()->str() == "&")
varInfo.erase(arg->varId());
} else if (var->second.managed()) {
Expand Down
8 changes: 4 additions & 4 deletions lib/checkleakautovar.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CPPCHECKLIB VarInfo {
};
enum Usage { USED, NORET };
std::map<int, AllocInfo> alloctype;
std::map<int, std::pair<std::string, Usage>> possibleUsage;
std::map<int, std::pair<const Token*, Usage>> possibleUsage;
std::set<int> conditionalAlloc;
std::set<int> referenced;

Expand Down Expand Up @@ -93,7 +93,7 @@ class CPPCHECKLIB VarInfo {
}

/** set possible usage for all variables */
void possibleUsageAll(const std::pair<std::string, Usage>& functionUsage);
void possibleUsageAll(const std::pair<const Token*, Usage>& functionUsage);

void print();
};
Expand Down Expand Up @@ -159,12 +159,12 @@ class CPPCHECKLIB CheckLeakAutoVar : public Check {
void doubleFreeError(const Token *tok, const Token *prevFreeTok, const std::string &varname, int type);

/** message: user configuration is needed to complete analysis */
void configurationInfo(const Token* tok, const std::pair<std::string, VarInfo::Usage>& functionUsage);
void configurationInfo(const Token* tok, const std::pair<const Token*, VarInfo::Usage>& functionUsage);

void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
CheckLeakAutoVar c(nullptr, settings, errorLogger);
c.deallocReturnError(nullptr, nullptr, "p");
c.configurationInfo(nullptr, { "f", VarInfo::USED }); // user configuration is needed to complete analysis
c.configurationInfo(nullptr, { nullptr, VarInfo::USED }); // user configuration is needed to complete analysis
c.doubleFreeError(nullptr, nullptr, "varname", 0);
}

Expand Down
10 changes: 10 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,16 @@ class TestLeakAutoVar : public TestFixture {
" SomeClass::someMethod(a);\n"
"}\n", settingsLeakIgnore);
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a\n", errout.str());

check("void bar(int* p) {\n"
" if (p)\n"
" free(p);\n"
"}\n"
"void f() {\n"
" int* p = malloc(4);\n"
" bar(p);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};

Expand Down

0 comments on commit a19142a

Please sign in to comment.