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

Method pointerArithmeticError

lib/checknullpointer.cpp:527–555  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

525}
526
527void CheckNullPointerImpl::pointerArithmeticError(const Token* tok, const ValueFlow::Value *value, bool inconclusive)
528{
529 // cppcheck-suppress shadowFunction - TODO: fix this
530 std::string arithmetic = arithmeticTypeString(tok);
531 std::string errmsg;
532 if (tok && tok->str()[0] == '-') {
533 errmsg = "Overflow in pointer arithmetic, NULL pointer is subtracted.";
534 } else {
535 errmsg = "Pointer " + arithmetic + " with NULL pointer.";
536 }
537
538 std::string id = "nullPointerArithmetic";
539 if (value && value->unknownFunctionReturn == ValueFlow::Value::UnknownFunctionReturn::outOfMemory) {
540 errmsg = "If memory allocation fails: " + (static_cast<char>(std::tolower(errmsg[0])) + errmsg.substr(1));
541 id += "OutOfMemory";
542 }
543 else if (value && value->unknownFunctionReturn == ValueFlow::Value::UnknownFunctionReturn::outOfResources) {
544 errmsg = "If resource allocation fails: " + (static_cast<char>(std::tolower(errmsg[0])) + errmsg.substr(1));
545 id += "OutOfResources";
546 }
547
548 ErrorPath errorPath = getErrorPath(tok, value, "Null pointer " + arithmetic);
549 reportError(std::move(errorPath),
550 Severity::error,
551 id.c_str(),
552 errmsg,
553 CWE_INCORRECT_CALCULATION,
554 inconclusive ? Certainty::inconclusive : Certainty::normal);
555}
556
557void CheckNullPointerImpl::redundantConditionWarning(const Token* tok, const ValueFlow::Value *value, const Token *condition, bool inconclusive)
558{

Callers 1

getErrorMessagesMethod · 0.45

Calls 3

arithmeticTypeStringFunction · 0.85
reportErrorFunction · 0.70
strMethod · 0.45

Tested by

no test coverage detected