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

Method memsetInvalid2ndParam

lib/checkfunctions.cpp:560–606  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

558}
559
560void CheckFunctionsImpl::memsetInvalid2ndParam()
561{
562// FIXME:
563// Replace this with library configuration.
564// For instance:
565// <arg nr="2">
566// <not-float/>
567// <warn possibleIntValue=":-129,256:" severity="warning" msg="..."/>
568// </arg>
569
570 const bool printPortability = mSettings.severity.isEnabled(Severity::portability);
571 const bool printWarning = mSettings.severity.isEnabled(Severity::warning);
572 if (!printWarning && !printPortability)
573 return;
574
575 logChecker("CheckFunctions::memsetInvalid2ndParam"); // warning,portability
576
577 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
578 for (const Scope *scope : symbolDatabase->functionScopes) {
579 for (const Token* tok = scope->bodyStart->next(); tok && (tok != scope->bodyEnd); tok = tok->next()) {
580 if (!Token::simpleMatch(tok, "memset ("))
581 continue;
582
583 const std::vector<const Token *> args = getArguments(tok);
584 if (args.size() != 3)
585 continue;
586
587 // Second parameter is zero literal, i.e. 0.0f
588 const Token * const secondParamTok = args[1];
589 if (Token::Match(secondParamTok, "%num% ,") && MathLib::isNullValue(secondParamTok->str()))
590 continue;
591
592 // Check if second parameter is a float variable or a float literal != 0.0f
593 if (printPortability && astIsFloat(secondParamTok,false)) {
594 memsetFloatError(secondParamTok, secondParamTok->expressionString());
595 }
596
597 if (printWarning && secondParamTok->isNumber()) { // Check if the second parameter is a literal and is out of range
598 const MathLib::bigint value = MathLib::toBigNumber(secondParamTok);
599 const long long sCharMin = mSettings.platform.signedCharMin();
600 const long long uCharMax = mSettings.platform.unsignedCharMax();
601 if (value < sCharMin || value > uCharMax)
602 memsetValueOutOfRangeError(secondParamTok, secondParamTok->str());
603 }
604 }
605 }
606}
607
608void CheckFunctionsImpl::memsetFloatError(const Token *tok, const std::string &var_value)
609{

Callers 1

runChecksMethod · 0.45

Calls 8

nextMethod · 0.80
simpleMatchFunction · 0.70
getArgumentsFunction · 0.70
astIsFloatFunction · 0.70
isEnabledMethod · 0.45
sizeMethod · 0.45
strMethod · 0.45
expressionStringMethod · 0.45

Tested by

no test coverage detected