forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckleakautovar.cpp
576 lines (499 loc) · 22.8 KB
/
checkleakautovar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// Leaks when using auto variables
//---------------------------------------------------------------------------
#include "checkleakautovar.h"
#include "checkmemoryleak.h" // <- CheckMemoryLeak::memoryLeak
#include "checkother.h" // <- doubleFreeError
#include "tokenize.h"
#include "errorlogger.h"
#include "symboldatabase.h"
#include <fstream>
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace {
CheckLeakAutoVar instance;
}
//---------------------------------------------------------------------------
void VarInfo::print()
{
std::cout << "size=" << alloctype.size() << std::endl;
std::map<unsigned int, std::string>::const_iterator it;
for (it = alloctype.begin(); it != alloctype.end(); ++it) {
std::string strusage;
std::map<unsigned int, std::string>::const_iterator use = possibleUsage.find(it->first);
if (use != possibleUsage.end())
strusage = use->second;
std::cout << "alloctype='" << it->second << "' "
<< "possibleUsage='" << strusage << "'" << std::endl;
}
}
void VarInfo::possibleUsageAll(const std::string &functionName)
{
possibleUsage.clear();
std::map<unsigned int, std::string>::const_iterator it;
for (it = alloctype.begin(); it != alloctype.end(); ++it)
possibleUsage[it->first] = functionName;
}
void CheckLeakAutoVar::leakError(const Token *tok, const std::string &varname)
{
const Standards standards;
CheckMemoryLeak checkmemleak(_tokenizer, _errorLogger, standards);
checkmemleak.memleakError(tok, varname);
//reportError(tok, Severity::error, "newleak", "New memory leak: " + varname);
}
void CheckLeakAutoVar::mismatchError(const Token *tok, const std::string &varname)
{
const Standards standards;
CheckMemoryLeak c(_tokenizer, _errorLogger, standards);
std::list<const Token *> callstack;
callstack.push_back(tok);
c.mismatchAllocDealloc(callstack, varname);
//reportError(tok, Severity::error, "newmismatch", "New mismatching allocation and deallocation: " + varname);
}
void CheckLeakAutoVar::deallocUseError(const Token *tok, const std::string &varname)
{
const Standards standards;
CheckMemoryLeak c(_tokenizer, _errorLogger, standards);
c.deallocuseError(tok, varname);
//reportError(tok, Severity::error, "newdeallocuse", "Using deallocated pointer " + varname);
}
void CheckLeakAutoVar::deallocReturnError(const Token *tok, const std::string &varname)
{
reportError(tok, Severity::error, "deallocret", "Returning/dereferencing '" + varname + "' after it is deallocated / released");
}
void CheckLeakAutoVar::configurationInfo(const Token* tok, const std::string &functionName)
{
if (((!cfgalloc.empty() || !cfgdealloc.empty()) && _settings->isEnabled("style")) || _settings->experimental) {
reportError(tok,
Severity::information,
"leakconfiguration",
functionName + " configuration is needed to establish if there is a leak or not");
}
}
void CheckLeakAutoVar::parseConfigurationFile(const std::string &filename)
{
std::ifstream fin(filename.c_str());
if (!fin.is_open())
return;
std::string line;
while (std::getline(fin,line)) {
if (line.compare(0,4,"MEM ",0,4) == 0) {
std::string f1;
enum {ALLOC, DEALLOC} type = ALLOC;
std::string::size_type pos1 = line.find_first_not_of(" ", 4U);
while (pos1 < line.size()) {
const std::string::size_type pos2 = line.find(" ", pos1);
std::string f;
if (pos2 == std::string::npos)
f = line.substr(pos1);
else
f = line.substr(pos1, pos2-pos1);
if (f1.empty())
f1 = f;
if (f == ":")
type = DEALLOC;
else if (type == ALLOC)
cfgalloc[f] = f1;
else if (type == DEALLOC)
cfgdealloc[f] = f1;
pos1 = line.find_first_not_of(" ", pos2);
}
}
else if (line.compare(0,7,"IGNORE ",0,7) == 0) {
std::string::size_type pos1 = line.find_first_not_of(" ", 7U);
while (pos1 < line.size()) {
std::string::size_type pos2 = line.find_first_of(" ", pos1);
std::string functionName;
if (pos2 == std::string::npos)
functionName = line.substr(pos1);
else
functionName = line.substr(pos1, pos2-pos1);
cfgignore.insert(functionName);
pos1 = line.find_first_not_of(" ", pos2);
}
}
else if (line.compare(0,4,"USE ",0,4) == 0) {
std::string::size_type pos1 = line.find_first_not_of(" ", 4U);
while (pos1 < line.size()) {
std::string::size_type pos2 = line.find_first_of(" ", pos1);
std::string functionName;
if (pos2 == std::string::npos)
functionName = line.substr(pos1);
else
functionName = line.substr(pos1, pos2-pos1);
cfguse.insert(functionName);
pos1 = line.find_first_not_of(" ", pos2);
}
}
else if (line.compare(0,9,"NORETURN ",0,9) == 0) {
std::string::size_type pos1 = line.find_first_not_of(" ", 9U);
while (pos1 < line.size()) {
std::string::size_type pos2 = line.find_first_of(" ", pos1);
std::string functionName;
if (pos2 == std::string::npos)
functionName = line.substr(pos1);
else
functionName = line.substr(pos1, pos2-pos1);
cfgnoreturn.insert(functionName);
pos1 = line.find_first_not_of(" ", pos2);
}
}
}
}
void CheckLeakAutoVar::check()
{
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
// Check function scopes
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) {
if (i->type == Scope::eFunction) {
// Empty variable info
VarInfo varInfo;
// Local variables that are known to be non-zero.
const std::set<unsigned int> notzero;
checkScope(i->classStart, &varInfo, notzero);
varInfo.conditionalAlloc.clear();
// Clear reference arguments from varInfo..
std::map<unsigned int, std::string>::iterator it = varInfo.alloctype.begin();
while (it != varInfo.alloctype.end()) {
const Variable *var = symbolDatabase->getVariableFromVarId(it->first);
if (!var ||
(var->isArgument() && var->isReference()) ||
(!var->isArgument() && !var->isLocal()))
varInfo.alloctype.erase(it++);
else
++it;
}
ret(i->classEnd, varInfo);
}
}
}
void CheckLeakAutoVar::checkScope(const Token * const startToken,
VarInfo *varInfo,
std::set<unsigned int> notzero)
{
std::map<unsigned int, std::string> &alloctype = varInfo->alloctype;
std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;
const std::set<unsigned int> conditionalAlloc(varInfo->conditionalAlloc);
// Allocation functions. key = function name, value = allocation type
std::map<std::string, std::string> allocFunctions(cfgalloc);
allocFunctions["malloc"] = "malloc";
allocFunctions["strdup"] = "malloc";
allocFunctions["fopen"] = "fopen";
// Deallocation functions. key = function name, value = allocation type
std::map<std::string, std::string> deallocFunctions(cfgdealloc);
deallocFunctions["free"] = "malloc";
deallocFunctions["fclose"] = "fopen";
// Parse all tokens
const Token * const endToken = startToken->link();
for (const Token *tok = startToken; tok && tok != endToken; tok = tok->next()) {
// Deallocation and then dereferencing pointer..
if (tok->varId() > 0) {
const std::map<unsigned int, std::string>::iterator var = alloctype.find(tok->varId());
if (var != alloctype.end()) {
if (var->second == "dealloc" && !Token::Match(tok->previous(), "[;{},=] %var% =")) {
deallocUseError(tok, tok->str());
} else if (Token::simpleMatch(tok->tokAt(-2), "= &")) {
varInfo->erase(tok->varId());
} else if (Token::simpleMatch(tok->previous(), "=")) {
varInfo->erase(tok->varId());
}
}
}
if (tok->str() == "(" && tok->previous()->isName()) {
functionCall(tok->previous(), varInfo, "");
tok = tok->link();
continue;
}
// look for end of statement
if (!Token::Match(tok, "[;{}]") || Token::Match(tok->next(), "[;{}]"))
continue;
tok = tok->next();
if (tok == endToken)
break;
// parse statement
// assignment..
if (tok && tok->varId() && Token::Match(tok, "%var% =")) {
// taking address of another variable..
if (Token::Match(tok->next(), "= %var% [+;]")) {
if (tok->tokAt(2)->varId() != tok->varId()) {
// If variable points at allocated memory => error
leakIfAllocated(tok, *varInfo);
// no multivariable checking currently => bail out for rhs variables
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == ";") {
break;
}
if (tok2->varId()) {
varInfo->erase(tok2->varId());
}
}
}
}
// is variable used in rhs?
bool used_in_rhs = false;
for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) {
if (tok2->str() == ";") {
break;
}
if (tok->varId() == tok2->varId()) {
used_in_rhs = true;
break;
}
}
// TODO: Better checking how the pointer is used in rhs?
if (used_in_rhs)
continue;
// Variable has already been allocated => error
if (conditionalAlloc.find(tok->varId()) == conditionalAlloc.end())
leakIfAllocated(tok, *varInfo);
varInfo->erase(tok->varId());
// not a local variable nor argument?
const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(tok->varId());
if (var && !var->isArgument() && !var->isLocal()) {
continue;
}
// Don't check reference variables
if (var && var->isReference())
continue;
// allocation?
if (Token::Match(tok->tokAt(2), "%type% (")) {
const std::map<std::string, std::string>::const_iterator it = allocFunctions.find(tok->strAt(2));
if (it != allocFunctions.end()) {
alloctype[tok->varId()] = it->second;
}
}
// Assigning non-zero value variable. It might be used to
// track the execution for a later if condition.
if (Token::Match(tok->tokAt(2), "%num% ;") && MathLib::toLongNumber(tok->strAt(2)) != 0)
notzero.insert(tok->varId());
else if (Token::Match(tok->tokAt(2), "- %type% ;") && tok->tokAt(3)->isUpperCaseName())
notzero.insert(tok->varId());
else
notzero.erase(tok->varId());
}
// if/else
else if (Token::simpleMatch(tok, "if (")) {
// Parse function calls inside the condition
for (const Token *innerTok = tok->tokAt(2); innerTok; innerTok = innerTok->next()) {
if (innerTok->str() == ")")
break;
if (innerTok->str() == "(" && innerTok->previous()->isName()) {
std::string dealloc;
{
const std::map<std::string, std::string>::iterator func = deallocFunctions.find(tok->str());
if (func != deallocFunctions.end()) {
dealloc = func->second;
}
}
functionCall(innerTok->previous(), varInfo, dealloc);
innerTok = innerTok->link();
}
}
const Token *tok2 = tok->linkAt(1);
if (Token::simpleMatch(tok2, ") {")) {
VarInfo varInfo1(*varInfo);
VarInfo varInfo2(*varInfo);
if (Token::Match(tok->next(), "( %var% )")) {
varInfo2.erase(tok->tokAt(2)->varId());
if (notzero.find(tok->tokAt(2)->varId()) != notzero.end())
varInfo2.clear();
} else if (Token::Match(tok->next(), "( ! %var% )|&&")) {
varInfo1.erase(tok->tokAt(3)->varId());
} else if (Token::Match(tok->next(), "( %var% ( ! %var% ) )|&&")) {
varInfo1.erase(tok->tokAt(5)->varId());
}
checkScope(tok2->next(), &varInfo1, notzero);
tok2 = tok2->linkAt(1);
if (Token::simpleMatch(tok2, "} else {")) {
checkScope(tok2->tokAt(2), &varInfo2, notzero);
tok = tok2->linkAt(2)->previous();
} else {
tok = tok2->previous();
}
VarInfo old;
old.swap(*varInfo);
// Conditional allocation in varInfo1
std::map<unsigned int, std::string>::const_iterator it;
for (it = varInfo1.alloctype.begin(); it != varInfo1.alloctype.end(); ++it) {
if (varInfo2.alloctype.find(it->first) == varInfo2.alloctype.end() &&
old.alloctype.find(it->first) == old.alloctype.end()) {
varInfo->conditionalAlloc.insert(it->first);
}
}
// Conditional allocation in varInfo2
for (it = varInfo2.alloctype.begin(); it != varInfo2.alloctype.end(); ++it) {
if (varInfo1.alloctype.find(it->first) == varInfo1.alloctype.end() &&
old.alloctype.find(it->first) == old.alloctype.end()) {
varInfo->conditionalAlloc.insert(it->first);
}
}
// Conditional allocation/deallocation
for (it = varInfo1.alloctype.begin(); it != varInfo1.alloctype.end(); ++it) {
if (it->second == "dealloc" && conditionalAlloc.find(it->first) != conditionalAlloc.end()) {
varInfo->conditionalAlloc.erase(it->first);
varInfo2.erase(it->first);
}
}
for (it = varInfo2.alloctype.begin(); it != varInfo2.alloctype.end(); ++it) {
if (it->second == "dealloc" && conditionalAlloc.find(it->first) != conditionalAlloc.end()) {
varInfo->conditionalAlloc.erase(it->first);
varInfo1.erase(it->first);
}
}
alloctype.insert(varInfo1.alloctype.begin(), varInfo1.alloctype.end());
alloctype.insert(varInfo2.alloctype.begin(), varInfo2.alloctype.end());
possibleUsage.insert(varInfo1.possibleUsage.begin(), varInfo1.possibleUsage.end());
possibleUsage.insert(varInfo2.possibleUsage.begin(), varInfo2.possibleUsage.end());
}
}
// unknown control..
else if (Token::Match(tok, "%type% (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
varInfo->clear();
break;
}
// Function call..
else if (Token::Match(tok, "%type% (") && tok->str() != "return") {
std::string dealloc;
{
const std::map<std::string, std::string>::iterator func = deallocFunctions.find(tok->str());
if (func != deallocFunctions.end()) {
dealloc = func->second;
}
}
functionCall(tok, varInfo, dealloc);
tok = tok->next()->link();
// Handle scopes that might be noreturn
if (dealloc.empty() && Token::simpleMatch(tok, ") ; }")) {
const std::string &functionName(tok->link()->previous()->str());
bool unknown = false;
if (cfgignore.find(functionName) == cfgignore.end() &&
cfguse.find(functionName) == cfguse.end() &&
_tokenizer->IsScopeNoReturn(tok->tokAt(2), &unknown)) {
if (unknown) {
//const std::string &functionName(tok->link()->previous()->str());
varInfo->possibleUsageAll(functionName);
} else {
varInfo->clear();
}
}
}
continue;
}
// return
else if (tok->str() == "return" || tok->str() == "throw") {
ret(tok, *varInfo);
varInfo->clear();
}
}
}
void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const std::string &dealloc)
{
std::map<unsigned int, std::string> &alloctype = varInfo->alloctype;
std::map<unsigned int, std::string> &possibleUsage = varInfo->possibleUsage;
// Ignore function call?
const bool ignore = bool(cfgignore.find(tok->str()) != cfgignore.end());
//const bool use = bool(cfguse.find(tok->str()) != cfguse.end());
if (ignore)
return;
for (const Token *arg = tok->tokAt(2); arg; arg = arg->nextArgument()) {
if ((Token::Match(arg, "%var% [-,)]") && arg->varId() > 0) ||
(Token::Match(arg, "& %var%") && arg->next()->varId() > 0)) {
// goto variable
if (arg->str() == "&")
arg = arg->next();
// Is variable allocated?
const std::map<unsigned int,std::string>::iterator var = alloctype.find(arg->varId());
if (var != alloctype.end()) {
if (dealloc.empty()) {
// possible usage
possibleUsage[arg->varId()] = tok->str();
} else if (var->second == "dealloc") {
CheckOther checkOther(_tokenizer, _settings, _errorLogger);
checkOther.doubleFreeError(tok, arg->str());
} else if (var->second != dealloc) {
// mismatching allocation and deallocation
mismatchError(tok, arg->str());
varInfo->erase(arg->varId());
} else {
// deallocation
var->second = "dealloc";
}
} else if (!dealloc.empty()) {
alloctype[arg->varId()] = "dealloc";
}
} else if (Token::Match(arg, "%var% (")) {
functionCall(arg, varInfo, dealloc);
}
}
}
void CheckLeakAutoVar::leakIfAllocated(const Token *vartok,
const VarInfo &varInfo)
{
const std::map<unsigned int, std::string> &alloctype = varInfo.alloctype;
const std::map<unsigned int, std::string> &possibleUsage = varInfo.possibleUsage;
const std::map<unsigned int,std::string>::const_iterator var = alloctype.find(vartok->varId());
if (var != alloctype.end() && var->second != "dealloc") {
const std::map<unsigned int, std::string>::const_iterator use = possibleUsage.find(vartok->varId());
if (use == possibleUsage.end()) {
leakError(vartok, vartok->str());
} else {
configurationInfo(vartok, use->second);
}
}
}
void CheckLeakAutoVar::ret(const Token *tok, const VarInfo &varInfo)
{
const std::map<unsigned int, std::string> &alloctype = varInfo.alloctype;
const std::map<unsigned int, std::string> &possibleUsage = varInfo.possibleUsage;
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::map<unsigned int, std::string>::const_iterator it = alloctype.begin(); it != alloctype.end(); ++it) {
// don't warn if variable is conditionally allocated
if (it->second != "dealloc" && varInfo.conditionalAlloc.find(it->first) != varInfo.conditionalAlloc.end())
continue;
const unsigned int varid = it->first;
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
if (var) {
bool used = false;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == ";")
break;
if (Token::Match(tok2, "return|(|, %varid% [);,]", varid)) {
used = true;
break;
}
if (Token::Match(tok2, "return|(|, & %varid% . %var% [);,]", varid)) {
used = true;
break;
}
}
// return deallocated pointer
if (used && it->second == "dealloc")
deallocReturnError(tok, var->name());
else if (!used && it->second != "dealloc") {
const std::map<unsigned int, std::string>::const_iterator use = possibleUsage.find(varid);
if (use == possibleUsage.end()) {
leakError(tok, var->name());
} else {
configurationInfo(tok, use->second);
}
}
}
}
}