MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / getDeallocationType

Method getDeallocationType

lib/checkmemoryleak.cpp:187–240  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

185
186
187CheckMemoryLeakImpl::AllocType CheckMemoryLeakImpl::getDeallocationType(const Token *tok, nonneg int varid) const
188{
189 if (tok->isCpp() && tok->str() == "delete" && tok->astOperand1()) {
190 const Token* vartok = tok->astOperand1();
191 if (Token::Match(vartok, ".|::"))
192 vartok = vartok->astOperand2();
193
194 if (vartok && vartok->varId() == varid) {
195 if (tok->strAt(1) == "[")
196 return NewArray;
197 return New;
198 }
199 }
200
201 if (tok->str() == "::")
202 tok = tok->next();
203
204 if (Token::Match(tok, "%name% (")) {
205 if (Token::simpleMatch(tok, "fcloseall ( )"))
206 return File;
207
208 int argNr = 1;
209 for (const Token* tok2 = tok->tokAt(2); tok2; tok2 = tok2->nextArgument()) {
210 const Token* vartok = tok2;
211 while (Token::Match(vartok, "%name% .|::"))
212 vartok = vartok->tokAt(2);
213
214 if (Token::Match(vartok, "%varid% )|,|-", varid)) {
215 if (tok->str() == "realloc" && Token::simpleMatch(vartok->next(), ", 0 )"))
216 return Malloc;
217
218 if (mSettings.hasLib("posix")) {
219 if (tok->str() == "close")
220 return Fd;
221 if (tok->str() == "pclose")
222 return Pipe;
223 }
224
225 // Does tok point on a Library deallocation function?
226 const int dealloctype = mSettings.library.getDeallocId(tok, argNr);
227 if (dealloctype > 0) {
228 if (dealloctype == mSettings.library.deallocId("free"))
229 return Malloc;
230 if (dealloctype == mSettings.library.deallocId("fclose"))
231 return File;
232 return Library::ismemory(dealloctype) ? OtherMem : OtherRes;
233 }
234 }
235 argNr++;
236 }
237 }
238
239 return No;
240}
241
242bool CheckMemoryLeakImpl::isReopenStandardStream(const Token *tok) const
243{

Callers

nothing calls this directly

Calls 10

ismemoryFunction · 0.85
astOperand1Method · 0.80
astOperand2Method · 0.80
nextMethod · 0.80
getDeallocIdMethod · 0.80
deallocIdMethod · 0.80
simpleMatchFunction · 0.70
strMethod · 0.45
tokAtMethod · 0.45
nextArgumentMethod · 0.45

Tested by

no test coverage detected