Skip to content

Commit b26bfc9

Browse files
Use getAllocFuncInfo() (danmar#5176)
1 parent b259617 commit b26bfc9

File tree

8 files changed

+20
-14
lines changed

8 files changed

+20
-14
lines changed

lib/checkclass.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,8 @@ void CheckClass::copyconstructors()
499499
}
500500
}
501501
for (tok = func.functionScope->bodyStart; tok != func.functionScope->bodyEnd; tok = tok->next()) {
502-
if (Token::Match(tok, "%var% = new|malloc|g_malloc|g_try_malloc|realloc|g_realloc|g_try_realloc")) {
502+
if ((mTokenizer->isCPP() && Token::Match(tok, "%var% = new")) ||
503+
(Token::Match(tok, "%var% = %name% (") && (mSettings->library.getAllocFuncInfo(tok->tokAt(2)) || mSettings->library.getReallocFuncInfo(tok->tokAt(2))))) {
503504
allocatedVars.erase(tok->varId());
504505
} else if (Token::Match(tok, "%var% = %name% . %name% ;") && allocatedVars.find(tok->varId()) != allocatedVars.end()) {
505506
copiedVars.insert(tok);
@@ -1404,7 +1405,8 @@ void CheckClass::checkMemset()
14041405
const std::set<const Scope *> parsedTypes;
14051406
checkMemsetType(scope, tok, type, false, parsedTypes);
14061407
}
1407-
} else if (tok->variable() && tok->variable()->typeScope() && Token::Match(tok, "%var% = calloc|malloc|realloc|g_malloc|g_try_malloc|g_realloc|g_try_realloc (")) {
1408+
} else if (tok->variable() && tok->variable()->typeScope() && Token::Match(tok, "%var% = %name% (") &&
1409+
(mSettings->library.getAllocFuncInfo(tok->tokAt(2)) || mSettings->library.getReallocFuncInfo(tok->tokAt(2)))) {
14081410
const std::set<const Scope *> parsedTypes;
14091411
checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, parsedTypes);
14101412

@@ -1737,16 +1739,18 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
17371739
if (!end)
17381740
end = func->functionScope->bodyEnd;
17391741
for (const Token *tok = start; tok && (tok != end); tok = tok->next()) {
1740-
if (Token::Match(tok, "%var% = malloc|realloc|calloc|new") && isMemberVar(scope, tok))
1742+
if (((mTokenizer->isCPP() && Token::Match(tok, "%var% = new")) ||
1743+
(Token::Match(tok, "%var% = %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2)))) &&
1744+
isMemberVar(scope, tok))
17411745
return true;
17421746

17431747
// check for deallocating memory
17441748
const Token *var;
17451749
if (Token::Match(tok, "%name% ( %var%") && mSettings->library.getDeallocFuncInfo(tok))
17461750
var = tok->tokAt(2);
1747-
else if (Token::Match(tok, "delete [ ] %var%"))
1751+
else if (mTokenizer->isCPP() && Token::Match(tok, "delete [ ] %var%"))
17481752
var = tok->tokAt(3);
1749-
else if (Token::Match(tok, "delete %var%"))
1753+
else if (mTokenizer->isCPP() && Token::Match(tok, "delete %var%"))
17501754
var = tok->next();
17511755
else
17521756
continue;

lib/checkmemoryleak.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ void CheckMemoryLeakStructMember::check() const
693693
}
694694
}
695695

696-
bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable)
696+
bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable) const
697697
{
698698
if (!variable)
699699
return false;
@@ -702,7 +702,7 @@ bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable)
702702
for (const Token *tok2 = variable->nameToken(); tok2 && tok2 != variable->scope()->bodyEnd; tok2 = tok2->next()) {
703703
if (Token::Match(tok2, "= %varid% [;=]", declarationId))
704704
return false;
705-
if (Token::Match(tok2, "%varid% = malloc|kmalloc (", declarationId)) // TODO use library
705+
if (Token::Match(tok2, "%varid% = %name% (", declarationId) && mSettings->library.getAllocFuncInfo(tok2->tokAt(2)))
706706
alloc = true;
707707
}
708708
return alloc;

lib/checkmemoryleak.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class CPPCHECKLIB CheckMemoryLeakStructMember : private Check, private CheckMemo
279279
private:
280280

281281
/** Is local variable allocated with malloc? */
282-
static bool isMalloc(const Variable *variable);
282+
bool isMalloc(const Variable *variable) const;
283283

284284
void checkStructVariable(const Variable* const variable) const;
285285

lib/checkother.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,8 @@ void CheckOther::checkInvalidFree()
23102310
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
23112311

23122312
// Keep track of which variables were assigned addresses to newly-allocated memory
2313-
if (Token::Match(tok, "%var% = malloc|g_malloc|new")) {
2313+
if ((mTokenizer->isCPP() && Token::Match(tok, "%var% = new")) ||
2314+
(Token::Match(tok, "%var% = %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2)))) {
23142315
allocation.insert(std::make_pair(tok->varId(), tok->strAt(2)));
23152316
inconclusive.insert(std::make_pair(tok->varId(), false));
23162317
}

lib/checksizeof.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ void CheckSizeof::checkSizeofForPointerSize()
127127
// Once leaving those tests, it is mandatory to have:
128128
// - variable matching the used pointer
129129
// - tokVar pointing on the argument where sizeof may be used
130-
if (Token::Match(tok->tokAt(2), "malloc|alloca|calloc (")) {
130+
if (Token::Match(tok->tokAt(2), "%name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2))) {
131131
if (Token::Match(tok, "%var% ="))
132132
variable = tok;
133133
else if (tok->strAt(1) == ")" && Token::Match(tok->linkAt(1)->tokAt(-2), "%var% ="))
134134
variable = tok->linkAt(1)->tokAt(-2);
135-
else if (tok->link() && Token::Match(tok, "> ( malloc|alloca|calloc (") && Token::Match(tok->link()->tokAt(-3), "%var% ="))
135+
else if (tok->link() && Token::Match(tok, "> ( %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2)) && Token::Match(tok->link()->tokAt(-3), "%var% ="))
136136
variable = tok->link()->tokAt(-3);
137137
tokSize = tok->tokAt(4);
138138
tokFunc = tok->tokAt(2);

lib/checkunusedvar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
10281028
if (var) {
10291029
// Consider allocating memory separately because allocating/freeing alone does not constitute using the variable
10301030
if (var->mType == Variables::pointer &&
1031-
Token::Match(skipBrackets(tok->next()), "= new|malloc|calloc|kmalloc|kzalloc|kcalloc|strdup|strndup|vmalloc|g_new0|g_try_new|g_new|g_malloc|g_malloc0|g_try_malloc|g_try_malloc0|g_strdup|g_strndup|g_strdup_printf")) {
1031+
((mTokenizer->isCPP() && Token::simpleMatch(skipBrackets(tok->next()), "= new")) ||
1032+
(Token::Match(skipBrackets(tok->next()), "= %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2))))) {
10321033
variables.allocateMemory(varid, tok);
10331034
} else if (var->mType == Variables::pointer || var->mType == Variables::reference) {
10341035
variables.read(varid, tok);

test/testclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2891,7 +2891,7 @@ class TestClass : public TestFixture {
28912891

28922892
#define checkNoMemset(...) checkNoMemset_(__FILE__, __LINE__, __VA_ARGS__)
28932893
void checkNoMemset_(const char* file, int line, const char code[]) {
2894-
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).build();
2894+
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).library("std.cfg").build();
28952895
checkNoMemset_(file, line, code, settings);
28962896
}
28972897

test/testsizeof.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TestSizeof : public TestFixture {
3636
TestSizeof() : TestFixture("TestSizeof") {}
3737

3838
private:
39-
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).certainty(Certainty::inconclusive).build();
39+
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).certainty(Certainty::inconclusive).library("std.cfg").build();
4040

4141
void run() override {
4242
TEST_CASE(sizeofsizeof);

0 commit comments

Comments
 (0)