diff --git a/gui/checkthread.cpp b/gui/checkthread.cpp index 808ef8a5225..773aab95a6d 100644 --- a/gui/checkthread.cpp +++ b/gui/checkthread.cpp @@ -440,7 +440,7 @@ void CheckThread::parseClangErrors(const QString &tool, const QString &file0, QS const std::string f0 = file0.toStdString(); const std::string msg = e.message.toStdString(); const std::string id = e.errorId.toStdString(); - ErrorMessage errmsg(callstack, f0, e.severity, msg, id, Certainty::normal); + ErrorMessage errmsg(std::move(callstack), f0, e.severity, msg, id, Certainty::normal); mResult.reportErr(errmsg); } } diff --git a/lib/check.cpp b/lib/check.cpp index 29285f14833..58b8dbfe0e1 100644 --- a/lib/check.cpp +++ b/lib/check.cpp @@ -71,10 +71,10 @@ void Check::reportError(const std::list &callstack, Severity seve writeToErrorList(errmsg); } -void Check::reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) +void Check::reportError(ErrorPath errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) { // TODO: report debug warning when error is for a disabled severity - const ErrorMessage errmsg(errorPath, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty); + const ErrorMessage errmsg(std::move(errorPath), mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty); if (mErrorLogger) mErrorLogger->reportErr(errmsg); else diff --git a/lib/check.h b/lib/check.h index a3d3191b68c..4e3c5b9a4e3 100644 --- a/lib/check.h +++ b/lib/check.h @@ -142,15 +142,10 @@ class CPPCHECKLIB Check { reportError(callstack, severity, id, msg, cwe, certainty); } - /** report an error */ - void reportError(const std::list &callstack, Severity severity, const std::string &id, const std::string &msg) { - reportError(callstack, severity, id, msg, CWE(0U), Certainty::normal); - } - /** report an error */ void reportError(const std::list &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty); - void reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty); + void reportError(ErrorPath errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty); /** log checker */ void logChecker(const char id[]); diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 2ea5a3de0fa..8b9023d9473 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -695,7 +695,7 @@ void CheckAutoVariables::errorReturnDanglingLifetime(const Token *tok, const Val ErrorPath errorPath = val ? val->errorPath : ErrorPath(); std::string msg = "Returning " + lifetimeMessage(tok, val, errorPath); errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::error, "returnDanglingLifetime", msg + " that will be invalid when returning.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), Severity::error, "returnDanglingLifetime", msg + " that will be invalid when returning.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); } void CheckAutoVariables::errorInvalidLifetime(const Token *tok, const ValueFlow::Value* val) @@ -704,7 +704,7 @@ void CheckAutoVariables::errorInvalidLifetime(const Token *tok, const ValueFlow: ErrorPath errorPath = val ? val->errorPath : ErrorPath(); std::string msg = "Using " + lifetimeMessage(tok, val, errorPath); errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::error, "invalidLifetime", msg + " that is out of scope.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), Severity::error, "invalidLifetime", msg + " that is out of scope.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); } void CheckAutoVariables::errorDanglingTemporaryLifetime(const Token* tok, const ValueFlow::Value* val, const Token* tempTok) @@ -714,7 +714,7 @@ void CheckAutoVariables::errorDanglingTemporaryLifetime(const Token* tok, const std::string msg = "Using " + lifetimeMessage(tok, val, errorPath); errorPath.emplace_back(tempTok, "Temporary created here."); errorPath.emplace_back(tok, ""); - reportError(errorPath, + reportError(std::move(errorPath), Severity::error, "danglingTemporaryLifetime", msg + " that is a temporary.", @@ -730,21 +730,21 @@ void CheckAutoVariables::errorDanglngLifetime(const Token *tok, const ValueFlow: std::string msg = isStatic ? "Static" : "Non-local"; msg += " variable '" + tokName + "' will use " + lifetimeMessage(tok, val, errorPath); errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::error, "danglingLifetime", msg + ".", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), Severity::error, "danglingLifetime", msg + ".", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); } void CheckAutoVariables::errorDanglingTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive) { errorPath.emplace_back(tok, ""); reportError( - errorPath, Severity::error, "danglingTempReference", "Using reference to dangling temporary.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); + std::move(errorPath), Severity::error, "danglingTempReference", "Using reference to dangling temporary.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); } void CheckAutoVariables::errorReturnReference(const Token* tok, ErrorPath errorPath, bool inconclusive) { errorPath.emplace_back(tok, ""); reportError( - errorPath, Severity::error, "returnReference", "Reference to local variable returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); + std::move(errorPath), Severity::error, "returnReference", "Reference to local variable returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); } void CheckAutoVariables::errorDanglingReference(const Token *tok, const Variable *var, ErrorPath errorPath) @@ -753,14 +753,14 @@ void CheckAutoVariables::errorDanglingReference(const Token *tok, const Variable std::string varName = var ? var->name() : "y"; std::string msg = "Non-local reference variable '" + tokName + "' to local variable '" + varName + "'"; errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::error, "danglingReference", msg, CWE562, Certainty::normal); + reportError(std::move(errorPath), Severity::error, "danglingReference", msg, CWE562, Certainty::normal); } void CheckAutoVariables::errorReturnTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive) { errorPath.emplace_back(tok, ""); reportError( - errorPath, Severity::error, "returnTempReference", "Reference to temporary returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); + std::move(errorPath), Severity::error, "returnTempReference", "Reference to temporary returned.", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal); } void CheckAutoVariables::errorInvalidDeallocation(const Token *tok, const ValueFlow::Value *val) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 0c19bceb137..5886ccbf640 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -875,7 +875,7 @@ void CheckBufferOverrun::argumentSizeError(const Token *tok, const std::string & errorPath.emplace_back(paramVar->nameToken(), "Passing buffer '" + paramVar->name() + "' to function that is declared here"); errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::warning, "argumentSize", + reportError(std::move(errorPath), Severity::warning, "argumentSize", "$symbol:" + functionName + '\n' + "Buffer '" + paramExpression + "' is too small, the function '" + functionName + "' expects a bigger buffer in " + strParamNum + " argument", CWE_ARGUMENT_SIZE, Certainty::normal); } @@ -1014,7 +1014,7 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map &locationList = + std::list locationList = CTU::FileInfo::getErrorPath(CTU::FileInfo::InvalidValueType::bufferOverflow, unsafeUsage, callsMap, @@ -1042,7 +1042,7 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::mapisKnown(); - reportError(errorPath, inconclusive ? Severity::warning : Severity::error, "negativeMemoryAllocationSize", + reportError(std::move(errorPath), inconclusive ? Severity::warning : Severity::error, "negativeMemoryAllocationSize", msg, CWE131, inconclusive ? Certainty::inconclusive : Certainty::normal); } diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 4da5a5ca733..8c9cd789fc0 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2971,7 +2971,7 @@ void CheckClass::virtualFunctionCallInConstructorError( } } - reportError(errorPath, Severity::style, "virtualCallInConstructor", + reportError(std::move(errorPath), Severity::style, "virtualCallInConstructor", "Virtual function '" + funcname + "' is called from " + scopeFunctionTypeName + " '" + constructorName + "' at line " + std::to_string(lineNumber) + ". Dynamic binding is not used.", CWE(0U), Certainty::normal); } @@ -2989,7 +2989,7 @@ void CheckClass::pureVirtualFunctionCallInConstructorError( if (!errorPath.empty()) errorPath.back().second = purefuncname + " is a pure virtual function without body"; - reportError(errorPath, Severity::warning, "pureVirtualCall", + reportError(std::move(errorPath), Severity::warning, "pureVirtualCall", "$symbol:" + purefuncname +"\n" "Call of pure virtual function '$symbol' in " + scopeFunctionTypeName + ".\n" "Call of pure virtual function '$symbol' in " + scopeFunctionTypeName + ". The call will fail during runtime.", CWE(0U), Certainty::normal); @@ -3120,7 +3120,7 @@ void CheckClass::duplInheritedMembersError(const Token *tok1, const Token* tok2, const std::string message = "The " + std::string(derivedIsStruct ? "struct" : "class") + " '" + derivedName + "' defines member " + member + " with name '" + memberName + "' also defined in its parent " + std::string(baseIsStruct ? "struct" : "class") + " '" + baseName + "'."; - reportError(errorPath, Severity::warning, "duplInheritedMember", symbols + '\n' + message, CWE398, Certainty::normal); + reportError(std::move(errorPath), Severity::warning, "duplInheritedMember", symbols + '\n' + message, CWE398, Certainty::normal); } @@ -3230,7 +3230,7 @@ void CheckClass::overrideError(const Function *funcInBase, const Function *funcI errorPath.emplace_back(funcInDerived->tokenDef, char(std::toupper(funcType[0])) + funcType.substr(1) + " in derived class"); } - reportError(errorPath, Severity::style, "missingOverride", + reportError(std::move(errorPath), Severity::style, "missingOverride", "$symbol:" + functionName + "\n" "The " + funcType + " '$symbol' overrides a " + funcType + " in a base class but is not marked with a 'override' specifier.", CWE(0U) /* Unknown CWE! */, @@ -3254,7 +3254,7 @@ void CheckClass::uselessOverrideError(const Function *funcInBase, const Function } else errStr += "just delegates back to the base class."; - reportError(errorPath, Severity::style, "uselessOverride", + reportError(std::move(errorPath), Severity::style, "uselessOverride", "$symbol:" + functionName + errStr, CWE(0U) /* Unknown CWE! */, @@ -3525,10 +3525,10 @@ bool CheckClass::checkThisUseAfterFreeRecursive(const Scope *classScope, const F void CheckClass::thisUseAfterFree(const Token *self, const Token *free, const Token *use) { std::string selfPointer = self ? self->str() : "ptr"; - const ErrorPath errorPath = { ErrorPathItem(self, "Assuming '" + selfPointer + "' is used as 'this'"), ErrorPathItem(free, "Delete '" + selfPointer + "', invalidating 'this'"), ErrorPathItem(use, "Call method when 'this' is invalid") }; + ErrorPath errorPath = { ErrorPathItem(self, "Assuming '" + selfPointer + "' is used as 'this'"), ErrorPathItem(free, "Delete '" + selfPointer + "', invalidating 'this'"), ErrorPathItem(use, "Call method when 'this' is invalid") }; const std::string usestr = use ? use->str() : "x"; const std::string usemsg = use && use->function() ? ("Calling method '" + usestr + "()'") : ("Using member '" + usestr + "'"); - reportError(errorPath, Severity::warning, "thisUseAfterFree", + reportError(std::move(errorPath), Severity::warning, "thisUseAfterFree", "$symbol:" + selfPointer + "\n" + usemsg + " when 'this' might be invalid", CWE(0), Certainty::normal); diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index f6c5d023567..41d93bb1138 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -522,7 +522,7 @@ void CheckCondition::duplicateConditionError(const Token *tok1, const Token *tok std::string msg = "The if condition is the same as the previous if condition"; - reportError(errorPath, Severity::style, "duplicateCondition", msg, CWE398, Certainty::normal); + reportError(std::move(errorPath), Severity::style, "duplicateCondition", msg, CWE398, Certainty::normal); } void CheckCondition::multiCondition() @@ -590,7 +590,7 @@ void CheckCondition::oppositeElseIfConditionError(const Token *ifCond, const Tok errorPath.emplace_back(ifCond, "first condition"); errorPath.emplace_back(elseIfCond, "else if condition is opposite to first condition"); - reportError(errorPath, Severity::style, "multiCondition", errmsg.str(), CWE398, Certainty::normal); + reportError(std::move(errorPath), Severity::style, "multiCondition", errmsg.str(), CWE398, Certainty::normal); } //--------------------------------------------------------------------------- @@ -867,7 +867,7 @@ void CheckCondition::oppositeInnerConditionError(const Token *tok1, const Token* const std::string msg("Opposite inner '" + innerSmt + "' condition leads to a dead code block.\n" "Opposite inner '" + innerSmt + "' condition leads to a dead code block (outer condition is '" + s1 + "' and inner condition is '" + s2 + "')."); - reportError(errorPath, Severity::warning, "oppositeInnerCondition", msg, CWE398, Certainty::normal); + reportError(std::move(errorPath), Severity::warning, "oppositeInnerCondition", msg, CWE398, Certainty::normal); } void CheckCondition::identicalInnerConditionError(const Token *tok1, const Token* tok2, ErrorPath errorPath) @@ -882,7 +882,7 @@ void CheckCondition::identicalInnerConditionError(const Token *tok1, const Token const std::string msg("Identical inner '" + innerSmt + "' condition is always true.\n" "Identical inner '" + innerSmt + "' condition is always true (outer condition is '" + s1 + "' and inner condition is '" + s2 + "')."); - reportError(errorPath, Severity::warning, "identicalInnerCondition", msg, CWE398, Certainty::normal); + reportError(std::move(errorPath), Severity::warning, "identicalInnerCondition", msg, CWE398, Certainty::normal); } void CheckCondition::identicalConditionAfterEarlyExitError(const Token *cond1, const Token* cond2, ErrorPath errorPath) @@ -898,7 +898,7 @@ void CheckCondition::identicalConditionAfterEarlyExitError(const Token *cond1, c errorPath.emplace_back(cond1, "If condition '" + cond + "' is true, the function will return/exit"); errorPath.emplace_back(cond2, (isReturnValue ? "Returning identical expression '" : "Testing identical condition '") + cond + "'"); - reportError(errorPath, + reportError(std::move(errorPath), Severity::warning, "identicalConditionAfterEarlyExit", isReturnValue @@ -1353,12 +1353,12 @@ void CheckCondition::incorrectLogicOperatorError(const Token *tok, const std::st return; errors.emplace_back(tok, ""); if (always) - reportError(errors, Severity::warning, "incorrectLogicOperator", + reportError(std::move(errors), Severity::warning, "incorrectLogicOperator", "Logical disjunction always evaluates to true: " + condition + ".\n" "Logical disjunction always evaluates to true: " + condition + ". " "Are these conditions necessary? Did you intend to use && instead? Are the numbers correct? Are you comparing the correct variables?", CWE571, inconclusive ? Certainty::inconclusive : Certainty::normal); else - reportError(errors, Severity::warning, "incorrectLogicOperator", + reportError(std::move(errors), Severity::warning, "incorrectLogicOperator", "Logical conjunction always evaluates to false: " + condition + ".\n" "Logical conjunction always evaluates to false: " + condition + ". " "Are these conditions necessary? Did you intend to use || instead? Are the numbers correct? Are you comparing the correct variables?", CWE570, inconclusive ? Certainty::inconclusive : Certainty::normal); @@ -1647,8 +1647,8 @@ void CheckCondition::alwaysTrueFalseError(const Token* tok, const Token* conditi const std::string expr = tok ? tok->expressionString() : std::string("x"); const std::string conditionStr = (Token::simpleMatch(condition, "return") ? "Return value" : "Condition"); const std::string errmsg = conditionStr + " '" + expr + "' is always " + bool_to_string(alwaysTrue); - const ErrorPath errorPath = getErrorPath(tok, value, errmsg); - reportError(errorPath, + ErrorPath errorPath = getErrorPath(tok, value, errmsg); + reportError(std::move(errorPath), Severity::style, "knownConditionTrueFalse", errmsg, @@ -1880,7 +1880,7 @@ void CheckCondition::duplicateConditionalAssignError(const Token *condTok, const } reportError( - errors, Severity::style, "duplicateConditionalAssign", msg, CWE398, Certainty::normal); + std::move(errors), Severity::style, "duplicateConditionalAssign", msg, CWE398, Certainty::normal); } diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 5be1622764d..9c26490d027 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -463,12 +463,12 @@ void CheckNullPointer::nullPointerError(const Token *tok, const std::string &var if (!mSettings->isEnabled(value, inconclusive) && !mSettings->isPremiumEnabled("nullPointer")) return; - const ErrorPath errorPath = getErrorPath(tok, value, "Null pointer dereference"); + ErrorPath errorPath = getErrorPath(tok, value, "Null pointer dereference"); if (value->condition) { - reportError(errorPath, Severity::warning, "nullPointerRedundantCheck", errmsgcond, CWE_NULL_POINTER_DEREFERENCE, inconclusive || value->isInconclusive() ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), Severity::warning, "nullPointerRedundantCheck", errmsgcond, CWE_NULL_POINTER_DEREFERENCE, inconclusive || value->isInconclusive() ? Certainty::inconclusive : Certainty::normal); } else if (value->defaultArg) { - reportError(errorPath, Severity::warning, "nullPointerDefaultArg", errmsgdefarg, CWE_NULL_POINTER_DEREFERENCE, inconclusive || value->isInconclusive() ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), Severity::warning, "nullPointerDefaultArg", errmsgdefarg, CWE_NULL_POINTER_DEREFERENCE, inconclusive || value->isInconclusive() ? Certainty::inconclusive : Certainty::normal); } else { std::string errmsg = std::string(value->isKnown() ? "Null" : "Possible null") + " pointer dereference"; @@ -485,7 +485,7 @@ void CheckNullPointer::nullPointerError(const Token *tok, const std::string &var if (!varname.empty()) errmsg = "$symbol:" + varname + '\n' + errmsg + ": $symbol"; - reportError(errorPath, + reportError(std::move(errorPath), value->isKnown() ? Severity::error : Severity::warning, id.c_str(), errmsg, @@ -561,8 +561,8 @@ void CheckNullPointer::pointerArithmeticError(const Token* tok, const ValueFlow: id += "OutOfResources"; } - const ErrorPath errorPath = getErrorPath(tok, value, "Null pointer " + arithmetic); - reportError(errorPath, + ErrorPath errorPath = getErrorPath(tok, value, "Null pointer " + arithmetic); + reportError(std::move(errorPath), Severity::error, id.c_str(), errmsg, @@ -580,8 +580,8 @@ void CheckNullPointer::redundantConditionWarning(const Token* tok, const ValueFl } else { errmsg = ValueFlow::eitherTheConditionIsRedundant(condition) + " or there is pointer arithmetic with NULL pointer."; } - const ErrorPath errorPath = getErrorPath(tok, value, "Null pointer " + arithmetic); - reportError(errorPath, + ErrorPath errorPath = getErrorPath(tok, value, "Null pointer " + arithmetic); + reportError(std::move(errorPath), Severity::warning, "nullPointerArithmeticRedundantCheck", errmsg, @@ -667,7 +667,7 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo &ctu, const std:: break; ValueFlow::Value::UnknownFunctionReturn unknownFunctionReturn = ValueFlow::Value::UnknownFunctionReturn::no; - const std::list &locationList = + std::list locationList = CTU::FileInfo::getErrorPath(CTU::FileInfo::InvalidValueType::null, unsafeUsage, callsMap, @@ -689,12 +689,12 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo &ctu, const std:: message = "If resource allocation fails, then there is a possible null pointer dereference: " + unsafeUsage.myArgumentName; } - const ErrorMessage errmsg(locationList, - fi->file0, - warning ? Severity::warning : Severity::error, - message, - std::move(id), - CWE_NULL_POINTER_DEREFERENCE, Certainty::normal); + ErrorMessage errmsg(std::move(locationList), + fi->file0, + warning ? Severity::warning : Severity::error, + message, + std::move(id), + CWE_NULL_POINTER_DEREFERENCE, Certainty::normal); errorLogger.reportErr(errmsg); foundErrors = true; diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 956ba4d244c..fc9fc3a291c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -742,22 +742,22 @@ void CheckOther::redundantCopyError(const Token *tok1, const Token* tok2, const void CheckOther::redundantAssignmentError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive) { - const ErrorPath errorPath = { ErrorPathItem(tok1, var + " is assigned"), ErrorPathItem(tok2, var + " is overwritten") }; + ErrorPath errorPath = { ErrorPathItem(tok1, var + " is assigned"), ErrorPathItem(tok2, var + " is overwritten") }; if (inconclusive) - reportError(errorPath, Severity::style, "redundantAssignment", + reportError(std::move(errorPath), Severity::style, "redundantAssignment", "$symbol:" + var + "\n" "Variable '$symbol' is reassigned a value before the old one has been used if variable is no semaphore variable.\n" "Variable '$symbol' is reassigned a value before the old one has been used. Make sure that this variable is not used like a semaphore in a threading environment before simplifying this code.", CWE563, Certainty::inconclusive); else - reportError(errorPath, Severity::style, "redundantAssignment", + reportError(std::move(errorPath), Severity::style, "redundantAssignment", "$symbol:" + var + "\n" "Variable '$symbol' is reassigned a value before the old one has been used.", CWE563, Certainty::normal); } void CheckOther::redundantInitializationError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive) { - const ErrorPath errorPath = { ErrorPathItem(tok1, var + " is initialized"), ErrorPathItem(tok2, var + " is overwritten") }; - reportError(errorPath, Severity::style, "redundantInitialization", + ErrorPath errorPath = { ErrorPathItem(tok1, var + " is initialized"), ErrorPathItem(tok2, var + " is overwritten") }; + reportError(std::move(errorPath), Severity::style, "redundantInitialization", "$symbol:" + var + "\nRedundant initialization for '$symbol'. The initialized value is overwritten before it is read.", CWE563, inconclusive ? Certainty::inconclusive : Certainty::normal); @@ -765,8 +765,8 @@ void CheckOther::redundantInitializationError(const Token *tok1, const Token* to void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token* tok2, const std::string &var) { - const ErrorPath errorPath = { ErrorPathItem(tok1, "$symbol is assigned"), ErrorPathItem(tok2, "$symbol is overwritten") }; - reportError(errorPath, Severity::style, "redundantAssignInSwitch", + ErrorPath errorPath = { ErrorPathItem(tok1, "$symbol is assigned"), ErrorPathItem(tok2, "$symbol is overwritten") }; + reportError(std::move(errorPath), Severity::style, "redundantAssignInSwitch", "$symbol:" + var + "\n" "Variable '$symbol' is reassigned a value before the old one has been used. 'break;' missing?", CWE563, Certainty::normal); } @@ -775,7 +775,7 @@ void CheckOther::redundantAssignmentSameValueError(const Token *tok, const Value { auto errorPath = val->errorPath; errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::style, "redundantAssignment", + reportError(std::move(errorPath), Severity::style, "redundantAssignment", "$symbol:" + var + "\n" "Variable '$symbol' is assigned an expression that holds the same value.", CWE563, Certainty::normal); } @@ -1610,7 +1610,7 @@ void CheckOther::passedByValueError(const Variable* var, bool inconclusive, bool msg += "\nVariable '$symbol' is used to iterate by value. It could be declared as a const reference which is usually faster and recommended in C++."; else msg += "\nParameter '$symbol' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++."; - reportError(errorPath, Severity::performance, id.c_str(), msg, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), Severity::performance, id.c_str(), msg, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal); } static bool isVariableMutableInInitializer(const Token* start, const Token * end, nonneg int varid) @@ -1998,7 +1998,7 @@ void CheckOther::constVariableError(const Variable *var, const Function *functio id += "Pointer"; } - reportError(errorPath, Severity::style, id.c_str(), message, CWE398, Certainty::normal); + reportError(std::move(errorPath), Severity::style, id.c_str(), message, CWE398, Certainty::normal); } //--------------------------------------------------------------------------- @@ -2401,7 +2401,7 @@ void CheckOther::zerodivError(const Token *tok, const ValueFlow::Value *value) return; } - const ErrorPath errorPath = getErrorPath(tok, value, "Division by zero"); + ErrorPath errorPath = getErrorPath(tok, value, "Division by zero"); std::ostringstream errmsg; if (value->condition) { @@ -2411,7 +2411,7 @@ void CheckOther::zerodivError(const Token *tok, const ValueFlow::Value *value) } else errmsg << "Division by zero."; - reportError(errorPath, + reportError(std::move(errorPath), value->errorSeverity() ? Severity::error : Severity::warning, value->condition ? "zerodivcond" : "zerodiv", errmsg.str(), CWE369, value->isInconclusive() ? Certainty::inconclusive : Certainty::normal); @@ -2632,7 +2632,7 @@ void CheckOther::duplicateBranchError(const Token *tok1, const Token *tok2, Erro errors.emplace_back(tok2, ""); errors.emplace_back(tok1, ""); - reportError(errors, Severity::style, "duplicateBranch", "Found duplicate branches for 'if' and 'else'.\n" + reportError(std::move(errors), Severity::style, "duplicateBranch", "Found duplicate branches for 'if' and 'else'.\n" "Finding the same code in an 'if' and related 'else' branch is suspicious and " "might indicate a cut and paste or logic error. Please examine this code " "carefully to determine if it is correct.", CWE398, Certainty::inconclusive); @@ -2975,7 +2975,7 @@ void CheckOther::oppositeExpressionError(const Token *opTok, ErrorPath errors) const std::string& op = opTok ? opTok->str() : "&&"; - reportError(errors, Severity::style, "oppositeExpression", "Opposite expression on both sides of \'" + op + "\'.\n" + reportError(std::move(errors), Severity::style, "oppositeExpression", "Opposite expression on both sides of \'" + op + "\'.\n" "Finding the opposite expression on both sides of an operator is suspicious and might " "indicate a cut and paste or logic error. Please examine this code carefully to " "determine if it is correct.", CWE398, Certainty::normal); @@ -3003,7 +3003,7 @@ void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2, if (expr1 != expr2 && !Token::Match(tok1, "%num%|NULL|nullptr") && !Token::Match(tok2, "%num%|NULL|nullptr")) msg += " because '" + expr1 + "' and '" + expr2 + "' represent the same value"; - reportError(errors, Severity::style, id, msg + + reportError(std::move(errors), Severity::style, id, msg + (std::string(".\nFinding the same expression ") + (hasMultipleExpr ? "more than once in a condition" : "on both sides of an operator")) + " is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to " "determine if it is correct.", CWE398, Certainty::normal); @@ -3026,7 +3026,7 @@ void CheckOther::duplicateAssignExpressionError(const Token *tok1, const Token * void CheckOther::duplicateExpressionTernaryError(const Token *tok, ErrorPath errors) { errors.emplace_back(tok, ""); - reportError(errors, Severity::style, "duplicateExpressionTernary", "Same expression in both branches of ternary operator.\n" + reportError(std::move(errors), Severity::style, "duplicateExpressionTernary", "Same expression in both branches of ternary operator.\n" "Finding the same expression in both branches of ternary operator is suspicious as " "the same code is executed regardless of the condition.", CWE398, Certainty::normal); } @@ -3868,8 +3868,8 @@ void CheckOther::accessMovedError(const Token *tok, const std::string &varname, return; } const std::string errmsg("$symbol:" + varname + "\nAccess of " + kindString + " variable '$symbol'."); - const ErrorPath errorPath = getErrorPath(tok, value, errmsg); - reportError(errorPath, Severity::warning, errorId, errmsg, CWE672, inconclusive ? Certainty::inconclusive : Certainty::normal); + ErrorPath errorPath = getErrorPath(tok, value, errmsg); + reportError(std::move(errorPath), Severity::warning, errorId, errmsg, CWE672, inconclusive ? Certainty::inconclusive : Certainty::normal); } @@ -4071,7 +4071,7 @@ void CheckOther::shadowError(const Token *var, const Token *shadowed, const std: const std::string Type = char(std::toupper(type[0])) + type.substr(1); const std::string id = "shadow" + Type; const std::string message = "$symbol:" + varname + "\nLocal variable \'$symbol\' shadows outer " + type; - reportError(errorPath, Severity::style, id.c_str(), message, CWE398, Certainty::normal); + reportError(std::move(errorPath), Severity::style, id.c_str(), message, CWE398, Certainty::normal); } static bool isVariableExpression(const Token* tok) @@ -4198,8 +4198,8 @@ void CheckOther::knownArgumentError(const Token *tok, const Token *ftok, const V errmsg += "Constant literal calculation disable/hide variable expression '" + varexpr + "'."; } - const ErrorPath errorPath = getErrorPath(tok, value, errmsg); - reportError(errorPath, Severity::style, id, errmsg, CWE570, Certainty::normal); + ErrorPath errorPath = getErrorPath(tok, value, errmsg); + reportError(std::move(errorPath), Severity::style, id, errmsg, CWE570, Certainty::normal); } void CheckOther::checkKnownPointerToBool() @@ -4241,8 +4241,8 @@ void CheckOther::knownPointerToBoolError(const Token* tok, const ValueFlow::Valu std::string cond = bool_to_string(!!value->intvalue); const std::string& expr = tok->expressionString(); std::string errmsg = "Pointer expression '" + expr + "' converted to bool is always " + cond + "."; - const ErrorPath errorPath = getErrorPath(tok, value, errmsg); - reportError(errorPath, Severity::style, "knownPointerToBool", errmsg, CWE570, Certainty::normal); + ErrorPath errorPath = getErrorPath(tok, value, errmsg); + reportError(std::move(errorPath), Severity::style, "knownPointerToBool", errmsg, CWE570, Certainty::normal); } void CheckOther::checkComparePointers() @@ -4303,7 +4303,7 @@ void CheckOther::comparePointersError(const Token *tok, const ValueFlow::Value * } errorPath.emplace_back(tok, ""); reportError( - errorPath, Severity::error, id, verb + " pointers that point to different objects", CWE570, Certainty::normal); + std::move(errorPath), Severity::error, id, verb + " pointers that point to different objects", CWE570, Certainty::normal); } void CheckOther::checkModuloOfOne() diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index bdade21bd48..8c2290fc098 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -271,7 +271,7 @@ void CheckStl::outOfBoundsError(const Token *tok, const std::string &containerNa } } - reportError(errorPath, + reportError(std::move(errorPath), (containerSize && !containerSize->errorSeverity()) || (indexValue && !indexValue->errorSeverity()) ? Severity::warning : Severity::error, "containerOutOfBounds", "$symbol:" + containerName +"\n" + errmsg, @@ -1253,7 +1253,7 @@ void CheckStl::invalidContainerLoopError(const Token* tok, const Token* loopTok, const std::string msg = "Calling '" + method + "' while iterating the container is invalid."; errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::error, "invalidContainerLoop", msg, CWE664, Certainty::normal); + reportError(std::move(errorPath), Severity::error, "invalidContainerLoop", msg, CWE664, Certainty::normal); } void CheckStl::invalidContainerError(const Token *tok, const Token * /*contTok*/, const ValueFlow::Value *val, ErrorPath errorPath) @@ -1263,7 +1263,7 @@ void CheckStl::invalidContainerError(const Token *tok, const Token * /*contTok*/ errorPath.insert(errorPath.begin(), val->errorPath.cbegin(), val->errorPath.cend()); std::string msg = "Using " + lifetimeMessage(tok, val, errorPath); errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::error, "invalidContainer", msg + " that may be invalid.", CWE664, inconclusive ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), Severity::error, "invalidContainer", msg + " that may be invalid.", CWE664, inconclusive ? Certainty::inconclusive : Certainty::normal); } void CheckStl::invalidContainerReferenceError(const Token* tok, const Token* contTok, ErrorPath errorPath) @@ -1271,7 +1271,7 @@ void CheckStl::invalidContainerReferenceError(const Token* tok, const Token* con std::string name = contTok ? contTok->expressionString() : "x"; std::string msg = "Reference to " + name; errorPath.emplace_back(tok, ""); - reportError(errorPath, Severity::error, "invalidContainerReference", msg + " that may be invalid.", CWE664, Certainty::normal); + reportError(std::move(errorPath), Severity::error, "invalidContainerReference", msg + " that may be invalid.", CWE664, Certainty::normal); } void CheckStl::stlOutOfBounds() @@ -1394,7 +1394,7 @@ void CheckStl::negativeIndex() void CheckStl::negativeIndexError(const Token *tok, const ValueFlow::Value &index) { - const ErrorPath errorPath = getErrorPath(tok, &index, "Negative array index"); + ErrorPath errorPath = getErrorPath(tok, &index, "Negative array index"); std::ostringstream errmsg; if (index.condition) errmsg << ValueFlow::eitherTheConditionIsRedundant(index.condition) @@ -1403,7 +1403,7 @@ void CheckStl::negativeIndexError(const Token *tok, const ValueFlow::Value &inde errmsg << "Array index " << index.intvalue << " is out of bounds."; const auto severity = index.errorSeverity() && index.isKnown() ? Severity::error : Severity::warning; const auto certainty = index.isInconclusive() ? Certainty::inconclusive : Certainty::normal; - reportError(errorPath, severity, "negativeContainerIndex", errmsg.str(), CWE786, certainty); + reportError(std::move(errorPath), severity, "negativeContainerIndex", errmsg.str(), CWE786, certainty); } void CheckStl::erase() @@ -2537,16 +2537,16 @@ void CheckStl::dereferenceInvalidIteratorError(const Token* tok, const ValueFlow if (!mSettings->isEnabled(value, inconclusive)) return; - const ErrorPath errorPath = getErrorPath(tok, value, "Dereference of an invalid iterator"); + ErrorPath errorPath = getErrorPath(tok, value, "Dereference of an invalid iterator"); if (value->condition) { - reportError(errorPath, Severity::warning, "derefInvalidIteratorRedundantCheck", errmsgcond, CWE825, (inconclusive || value->isInconclusive()) ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), Severity::warning, "derefInvalidIteratorRedundantCheck", errmsgcond, CWE825, (inconclusive || value->isInconclusive()) ? Certainty::inconclusive : Certainty::normal); } else { std::string errmsg = std::string(value->isKnown() ? "Dereference" : "Possible dereference") + " of an invalid iterator"; if (!varname.empty()) errmsg = "$symbol:" + varname + '\n' + errmsg + ": $symbol"; - reportError(errorPath, + reportError(std::move(errorPath), value->isKnown() ? Severity::error : Severity::warning, "derefInvalidIterator", errmsg, diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 2bcfe99ea98..4b025d026e5 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -124,14 +124,14 @@ void CheckType::tooBigBitwiseShiftError(const Token *tok, int lhsbits, const Val return; } - const ErrorPath errorPath = getErrorPath(tok, &rhsbits, "Shift"); + ErrorPath errorPath = getErrorPath(tok, &rhsbits, "Shift"); std::ostringstream errmsg; errmsg << "Shifting " << lhsbits << "-bit value by " << rhsbits.intvalue << " bits is undefined behaviour"; if (rhsbits.condition) errmsg << ". See condition at line " << rhsbits.condition->linenr() << "."; - reportError(errorPath, rhsbits.errorSeverity() ? Severity::error : Severity::warning, id, errmsg.str(), CWE758, rhsbits.isInconclusive() ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), rhsbits.errorSeverity() ? Severity::error : Severity::warning, id, errmsg.str(), CWE758, rhsbits.isInconclusive() ? Certainty::inconclusive : Certainty::normal); } void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits) @@ -155,14 +155,14 @@ void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, con if ((severity == Severity::portability) && !mSettings->severity.isEnabled(Severity::portability)) return; - const ErrorPath errorPath = getErrorPath(tok, &rhsbits, "Shift"); + ErrorPath errorPath = getErrorPath(tok, &rhsbits, "Shift"); std::ostringstream errmsg; errmsg << "Shifting signed " << lhsbits << "-bit value by " << rhsbits.intvalue << " bits is " + behaviour + " behaviour"; if (rhsbits.condition) errmsg << ". See condition at line " << rhsbits.condition->linenr() << "."; - reportError(errorPath, severity, id, errmsg.str(), CWE758, rhsbits.isInconclusive() ? Certainty::inconclusive : Certainty::normal); + reportError(std::move(errorPath), severity, id, errmsg.str(), CWE758, rhsbits.isInconclusive() ? Certainty::inconclusive : Certainty::normal); } //--------------------------------------------------------------------------- @@ -294,8 +294,8 @@ void CheckType::signConversionError(const Token *tok, const ValueFlow::Value *ne if (!negativeValue) reportError(tok, Severity::warning, "signConversion", msg.str(), CWE195, Certainty::normal); else { - const ErrorPath &errorPath = getErrorPath(tok,negativeValue,"Negative value is converted to an unsigned value"); - reportError(errorPath, + ErrorPath errorPath = getErrorPath(tok,negativeValue,"Negative value is converted to an unsigned value"); + reportError(std::move(errorPath), Severity::warning, Check::getMessageId(*negativeValue, "signConversion").c_str(), msg.str(), diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index ddbdfee5496..41175218e48 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1563,7 +1563,7 @@ void CheckUninitVar::uninitvarError(const Token *tok, const std::string &varname if (diag(tok)) return; errorPath.emplace_back(tok, ""); - reportError(errorPath, + reportError(std::move(errorPath), Severity::error, "legacyUninitvar", "$symbol:" + varname + "\nUninitialized variable: $symbol", @@ -1586,7 +1586,7 @@ void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v) auto severity = v.isKnown() ? Severity::error : Severity::warning; auto certainty = v.isInconclusive() ? Certainty::inconclusive : Certainty::normal; if (v.subexpressions.empty()) { - reportError(errorPath, + reportError(std::move(errorPath), severity, "uninitvar", "$symbol:" + varname + "\nUninitialized variable: $symbol", @@ -1600,7 +1600,7 @@ void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v) vars += prefix + varname + "." + var; prefix = ", "; } - reportError(errorPath, + reportError(std::move(errorPath), severity, "uninitvar", "$symbol:" + varname + "\nUninitialized " + vars, @@ -1774,7 +1774,7 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo &ctu, const std::li for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeUsage) { const CTU::FileInfo::FunctionCall *functionCall = nullptr; - const std::list &locationList = + std::list locationList = CTU::FileInfo::getErrorPath(CTU::FileInfo::InvalidValueType::uninit, unsafeUsage, callsMap, @@ -1785,7 +1785,7 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo &ctu, const std::li if (locationList.empty()) continue; - const ErrorMessage errmsg(locationList, + const ErrorMessage errmsg(std::move(locationList), fi->file0, Severity::error, "Using argument " + unsafeUsage.myArgumentName + " that points at uninitialized variable " + functionCall->callArgumentExpression, diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 59f15a810fc..9e584b9ab79 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1589,7 +1589,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list) if (!re) { if (pcreCompileErrorStr) { const std::string msg = "pcre_compile failed: " + std::string(pcreCompileErrorStr); - const ErrorMessage errmsg(std::list(), + const ErrorMessage errmsg({}, "", Severity::error, msg, @@ -1610,7 +1610,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list) // It is NULL if everything works, and points to an error string otherwise. if (pcreStudyErrorStr) { const std::string msg = "pcre_study failed: " + std::string(pcreStudyErrorStr); - const ErrorMessage errmsg(std::list(), + const ErrorMessage errmsg({}, "", Severity::error, msg, @@ -1633,7 +1633,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list) if (pcreExecRet < 0) { const std::string errorMessage = pcreErrorCodeToString(pcreExecRet); if (!errorMessage.empty()) { - const ErrorMessage errmsg(std::list(), + const ErrorMessage errmsg({}, "", Severity::error, std::string("pcre_exec failed: ") + errorMessage, diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index e1650c3e670..b31e66c5c32 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -128,17 +128,17 @@ ErrorMessage::ErrorMessage(const std::list& callstack, const Token hash = 0; // calculateWarningHash(list, hashWarning.str()); } -ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenList, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) +ErrorMessage::ErrorMessage(ErrorPath errorPath, const TokenList *tokenList, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) : id(id), severity(severity), cwe(cwe.id), certainty(certainty) { // Format callstack - for (const ErrorPathItem& e: errorPath) { + for (ErrorPathItem& e: errorPath) { const Token *tok = e.first; // --errorlist can provide null values here if (!tok) continue; - const std::string& path_info = e.second; + std::string& path_info = e.second; std::string info; if (startsWith(path_info,"$symbol:") && path_info.find('\n') < path_info.size()) { @@ -147,7 +147,7 @@ ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenLis info = replaceStr(path_info.substr(pos+1), "$symbol", symbolName); } else { - info = path_info; + info = std::move(path_info); } callStack.emplace_back(tok, std::move(info), tokenList); diff --git a/lib/errorlogger.h b/lib/errorlogger.h index ab8c7556b28..86b2caf666a 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -126,7 +126,7 @@ class CPPCHECKLIB ErrorMessage { const std::string& msg, const CWE &cwe, Certainty certainty); - ErrorMessage(const ErrorPath &errorPath, + ErrorMessage(ErrorPath errorPath, const TokenList *tokenList, Severity severity, const char id[], diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index 40ec88c309d..354fe9909c9 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -431,8 +431,7 @@ class TestErrorLogger : public TestFixture { void ToXmlV2Encoding() const { { - std::list locs; - ErrorMessage msg(std::move(locs), "", Severity::error, "Programming error.\nComparing \"\203\" with \"\003\"", "errorId", Certainty::normal); + ErrorMessage msg({}, "", Severity::error, "Programming error.\nComparing \"\203\" with \"\003\"", "errorId", Certainty::normal); const std::string expected(" "); ASSERT_EQUALS(expected, msg.toXML()); } @@ -506,8 +505,7 @@ class TestErrorLogger : public TestFixture { void SerializeInconclusiveMessage() const { // Inconclusive error message - std::list locs; - ErrorMessage msg(std::move(locs), "", Severity::error, "Programming error", "errorId", Certainty::inconclusive); + ErrorMessage msg({}, "", Severity::error, "Programming error", "errorId", Certainty::inconclusive); msg.file0 = "test.cpp"; const std::string msg_str = msg.serialize(); @@ -605,8 +603,7 @@ class TestErrorLogger : public TestFixture { } void SerializeSanitize() const { - std::list locs; - ErrorMessage msg(std::move(locs), "", Severity::error, std::string("Illegal character in \"foo\001bar\""), "errorId", Certainty::normal); + ErrorMessage msg({}, "", Severity::error, std::string("Illegal character in \"foo\001bar\""), "errorId", Certainty::normal); msg.file0 = "1.c"; const std::string msg_str = msg.serialize();