avoid some unnecessary code execution (#4962)

This commit is contained in:
Oliver Stöneberg 2023-04-28 12:42:51 +02:00 committed by GitHub
parent 6eae4e71f6
commit 1cd1cbabe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 30 deletions

View File

@ -137,12 +137,6 @@ void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, con
return; return;
} }
const 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() << ".";
Severity::SeverityType severity = rhsbits.errorSeverity() ? Severity::error : Severity::warning; Severity::SeverityType severity = rhsbits.errorSeverity() ? Severity::error : Severity::warning;
if (cpp14) if (cpp14)
@ -150,6 +144,13 @@ void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, con
if ((severity == Severity::portability) && !mSettings->severity.isEnabled(Severity::portability)) if ((severity == Severity::portability) && !mSettings->severity.isEnabled(Severity::portability))
return; return;
const 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(errorPath, severity, id, errmsg.str(), CWE758, rhsbits.isInconclusive() ? Certainty::inconclusive : Certainty::normal);
} }

View File

@ -116,6 +116,10 @@ ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenLis
// Format callstack // Format callstack
for (const ErrorPathItem& e: errorPath) { for (const ErrorPathItem& e: errorPath) {
const Token *tok = e.first; const Token *tok = e.first;
// --errorlist can provide null values here
if (!tok)
continue;
std::string info = e.second; std::string info = e.second;
if (info.compare(0,8,"$symbol:") == 0 && info.find('\n') < info.size()) { if (info.compare(0,8,"$symbol:") == 0 && info.find('\n') < info.size()) {
@ -124,9 +128,7 @@ ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenLis
info = replaceStr(info.substr(pos+1), "$symbol", symbolName); info = replaceStr(info.substr(pos+1), "$symbol", symbolName);
} }
// --errorlist can provide null values here callStack.emplace_back(tok, info, tokenList);
if (tok)
callStack.emplace_back(tok, info, tokenList);
} }
if (tokenList && !tokenList->getFiles().empty()) if (tokenList && !tokenList->getFiles().empty())

View File

@ -3463,15 +3463,14 @@ const std::string& Type::name() const
void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, const std::string &msg) const void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, const std::string &msg) const
{ {
if (tok && mSettings.debugwarnings) { if (tok && mSettings.debugwarnings && mErrorLogger) {
const std::list<const Token*> locationList(1, tok); const std::list<const Token*> locationList(1, tok);
const ErrorMessage errmsg(locationList, &mTokenizer.list, const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::debug, Severity::debug,
type, type,
msg, msg,
Certainty::normal); Certainty::normal);
if (mErrorLogger) mErrorLogger->reportErr(errmsg);
mErrorLogger->reportErr(errmsg);
} }
} }

View File

@ -1144,15 +1144,14 @@ void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration)
while (it != eq.cend()) { while (it != eq.cend()) {
// check for end // check for end
if (!it->end) { if (!it->end) {
if (mSettings.debugwarnings) { if (mSettings.debugwarnings && mErrorLogger && mSettings.severity.isEnabled(Severity::debug)) {
const std::list<const Token*> locationList(1, it->eq); const std::list<const Token*> locationList(1, it->eq);
const ErrorMessage errmsg(locationList, &mTokenizer.list, const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::debug, Severity::debug,
"noparamend", "noparamend",
"TemplateSimplifier couldn't find end of template parameter.", "TemplateSimplifier couldn't find end of template parameter.",
Certainty::normal); Certainty::normal);
if (mErrorLogger && mSettings.severity.isEnabled(Severity::debug)) mErrorLogger->reportErr(errmsg);
mErrorLogger->reportErr(errmsg);
} }
break; break;
} }
@ -3045,20 +3044,21 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
numberOfTemplateInstantiations = mTemplateInstantiations.size(); numberOfTemplateInstantiations = mTemplateInstantiations.size();
++recursiveCount; ++recursiveCount;
if (recursiveCount > mSettings.maxTemplateRecursion) { if (recursiveCount > mSettings.maxTemplateRecursion) {
std::list<std::string> typeStringsUsedInTemplateInstantiation; if (mErrorLogger && mSettings.severity.isEnabled(Severity::information)) {
const std::string typeForNewName = templateDeclaration.name() + "<" + getNewName(instantiation.token(), typeStringsUsedInTemplateInstantiation) + ">"; std::list<std::string> typeStringsUsedInTemplateInstantiation;
const std::string typeForNewName = templateDeclaration.name() + "<" + getNewName(instantiation.token(), typeStringsUsedInTemplateInstantiation) + ">";
const std::list<const Token *> callstack(1, instantiation.token()); const std::list<const Token *> callstack(1, instantiation.token());
const ErrorMessage errmsg(callstack, const ErrorMessage errmsg(callstack,
&mTokenizer.list, &mTokenizer.list,
Severity::information, Severity::information,
"templateRecursion", "templateRecursion",
"TemplateSimplifier: max template recursion (" "TemplateSimplifier: max template recursion ("
+ MathLib::toString(mSettings.maxTemplateRecursion) + MathLib::toString(mSettings.maxTemplateRecursion)
+ ") reached for template '"+typeForNewName+"'. You might want to limit Cppcheck recursion.", + ") reached for template '"+typeForNewName+"'. You might want to limit Cppcheck recursion.",
Certainty::normal); Certainty::normal);
if (mErrorLogger && mSettings.severity.isEnabled(Severity::information))
mErrorLogger->reportErr(errmsg); mErrorLogger->reportErr(errmsg);
}
// bail out.. // bail out..
break; break;
@ -3896,15 +3896,14 @@ void TemplateSimplifier::simplifyTemplates(
} }
if (passCount == passCountMax) { if (passCount == passCountMax) {
if (mSettings.debugwarnings) { if (mSettings.debugwarnings && mErrorLogger) {
const std::list<const Token*> locationList(1, mTokenList.front()); const std::list<const Token*> locationList(1, mTokenList.front());
const ErrorMessage errmsg(locationList, &mTokenizer.list, const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::debug, Severity::debug,
"debug", "debug",
"TemplateSimplifier: pass count limit hit before simplifications were finished.", "TemplateSimplifier: pass count limit hit before simplifications were finished.",
Certainty::normal); Certainty::normal);
if (mErrorLogger) mErrorLogger->reportErr(errmsg);
mErrorLogger->reportErr(errmsg);
} }
} }