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

Method suspiciousSizeofCalculation

lib/checksizeof.cpp:398–426  ·  view source on GitHub ↗

----------------------------------------------------------------------------- Check for code like sizeof()*sizeof() or sizeof(ptr)/value -----------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

396// Check for code like sizeof()*sizeof() or sizeof(ptr)/value
397//-----------------------------------------------------------------------------
398void CheckSizeofImpl::suspiciousSizeofCalculation()
399{
400 if (!mSettings.severity.isEnabled(Severity::warning) || !mSettings.certainty.isEnabled(Certainty::inconclusive))
401 return;
402
403 logChecker("CheckSizeof::suspiciousSizeofCalculation"); // warning,inconclusive
404
405 for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
406 if (Token::simpleMatch(tok, "sizeof (")) {
407 const Token* lPar = tok->astParent();
408 if (lPar && lPar->str() == "(") {
409 const Token* const rPar = lPar->link();
410 const Token* varTok = lPar->astOperand2();
411 int derefCount = 0;
412 while (Token::Match(varTok, "[|*")) {
413 ++derefCount;
414 varTok = varTok->astOperand1();
415 }
416 if (lPar->astParent() && lPar->astParent()->str() == "/") {
417 const Variable* var = varTok ? varTok->variable() : nullptr;
418 if (var && var->isPointer() && !var->isArray() && !(var->valueType() && var->valueType()->pointer <= derefCount))
419 divideSizeofError(tok);
420 }
421 else if (Token::simpleMatch(rPar, ") * sizeof") && rPar->next()->astOperand1() == tok->next())
422 multiplySizeofError(tok);
423 }
424 }
425 }
426}
427
428void CheckSizeofImpl::multiplySizeofError(const Token *tok)
429{

Callers 1

runChecksMethod · 0.45

Calls 9

nextMethod · 0.80
astParentMethod · 0.80
astOperand2Method · 0.80
astOperand1Method · 0.80
variableMethod · 0.80
isArrayMethod · 0.80
simpleMatchFunction · 0.70
isEnabledMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected