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

Method arithmetic

lib/checknullpointer.cpp:480–516  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

478}
479
480void CheckNullPointerImpl::arithmetic()
481{
482 logChecker("CheckNullPointer::arithmetic");
483 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
484 for (const Scope * scope : symbolDatabase->functionScopes) {
485 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
486 if (!Token::Match(tok, "-|+|+=|-=|++|--"))
487 continue;
488 const Token *pointerOperand;
489 const Token *numericOperand;
490 if (tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->pointer != 0) {
491 pointerOperand = tok->astOperand1();
492 numericOperand = tok->astOperand2();
493 } else if (tok->astOperand2() && tok->astOperand2()->valueType() && tok->astOperand2()->valueType()->pointer != 0) {
494 pointerOperand = tok->astOperand2();
495 numericOperand = tok->astOperand1();
496 } else
497 continue;
498 if (numericOperand && numericOperand->valueType() && !numericOperand->valueType()->isIntegral())
499 continue;
500 const ValueFlow::Value* numValue = numericOperand ? numericOperand->getValue(0) : nullptr;
501 if (numValue && numValue->intvalue == 0) // don't warn for arithmetic with 0
502 continue;
503 const ValueFlow::Value* value = pointerOperand->getValue(0);
504 if (!value)
505 continue;
506 if (!mSettings.certainty.isEnabled(Certainty::inconclusive) && value->isInconclusive())
507 continue;
508 if (value->condition && !mSettings.severity.isEnabled(Severity::warning))
509 continue;
510 if (value->condition)
511 redundantConditionWarning(tok, value, value->condition, value->isInconclusive());
512 else
513 pointerArithmeticError(tok, value, value->isInconclusive());
514 }
515 }
516}
517
518static std::string arithmeticTypeString(const Token *tok)
519{

Callers 1

runChecksMethod · 0.80

Calls 7

nextMethod · 0.80
astOperand1Method · 0.80
astOperand2Method · 0.80
isIntegralMethod · 0.80
getValueMethod · 0.45
isEnabledMethod · 0.45
isInconclusiveMethod · 0.45

Tested by

no test coverage detected