Skip to content

Commit 1cd1cba

Browse files
authored
avoid some unnecessary code execution (#4962)
1 parent 6eae4e7 commit 1cd1cba

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

lib/checktype.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,20 @@ void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, con
137137
return;
138138
}
139139

140-
const ErrorPath errorPath = getErrorPath(tok, &rhsbits, "Shift");
141-
142-
std::ostringstream errmsg;
143-
errmsg << "Shifting signed " << lhsbits << "-bit value by " << rhsbits.intvalue << " bits is " + behaviour + " behaviour";
144-
if (rhsbits.condition)
145-
errmsg << ". See condition at line " << rhsbits.condition->linenr() << ".";
146140

147141
Severity::SeverityType severity = rhsbits.errorSeverity() ? Severity::error : Severity::warning;
148142
if (cpp14)
149143
severity = Severity::portability;
150144

151145
if ((severity == Severity::portability) && !mSettings->severity.isEnabled(Severity::portability))
152146
return;
147+
const ErrorPath errorPath = getErrorPath(tok, &rhsbits, "Shift");
148+
149+
std::ostringstream errmsg;
150+
errmsg << "Shifting signed " << lhsbits << "-bit value by " << rhsbits.intvalue << " bits is " + behaviour + " behaviour";
151+
if (rhsbits.condition)
152+
errmsg << ". See condition at line " << rhsbits.condition->linenr() << ".";
153+
153154
reportError(errorPath, severity, id, errmsg.str(), CWE758, rhsbits.isInconclusive() ? Certainty::inconclusive : Certainty::normal);
154155
}
155156

lib/errorlogger.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenLis
116116
// Format callstack
117117
for (const ErrorPathItem& e: errorPath) {
118118
const Token *tok = e.first;
119+
// --errorlist can provide null values here
120+
if (!tok)
121+
continue;
122+
119123
std::string info = e.second;
120124

121125
if (info.compare(0,8,"$symbol:") == 0 && info.find('\n') < info.size()) {
@@ -124,9 +128,7 @@ ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenLis
124128
info = replaceStr(info.substr(pos+1), "$symbol", symbolName);
125129
}
126130

127-
// --errorlist can provide null values here
128-
if (tok)
129-
callStack.emplace_back(tok, info, tokenList);
131+
callStack.emplace_back(tok, info, tokenList);
130132
}
131133

132134
if (tokenList && !tokenList->getFiles().empty())

lib/symboldatabase.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,15 +3463,14 @@ const std::string& Type::name() const
34633463

34643464
void SymbolDatabase::debugMessage(const Token *tok, const std::string &type, const std::string &msg) const
34653465
{
3466-
if (tok && mSettings.debugwarnings) {
3466+
if (tok && mSettings.debugwarnings && mErrorLogger) {
34673467
const std::list<const Token*> locationList(1, tok);
34683468
const ErrorMessage errmsg(locationList, &mTokenizer.list,
34693469
Severity::debug,
34703470
type,
34713471
msg,
34723472
Certainty::normal);
3473-
if (mErrorLogger)
3474-
mErrorLogger->reportErr(errmsg);
3473+
mErrorLogger->reportErr(errmsg);
34753474
}
34763475
}
34773476

lib/templatesimplifier.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,15 +1144,14 @@ void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration)
11441144
while (it != eq.cend()) {
11451145
// check for end
11461146
if (!it->end) {
1147-
if (mSettings.debugwarnings) {
1147+
if (mSettings.debugwarnings && mErrorLogger && mSettings.severity.isEnabled(Severity::debug)) {
11481148
const std::list<const Token*> locationList(1, it->eq);
11491149
const ErrorMessage errmsg(locationList, &mTokenizer.list,
11501150
Severity::debug,
11511151
"noparamend",
11521152
"TemplateSimplifier couldn't find end of template parameter.",
11531153
Certainty::normal);
1154-
if (mErrorLogger && mSettings.severity.isEnabled(Severity::debug))
1155-
mErrorLogger->reportErr(errmsg);
1154+
mErrorLogger->reportErr(errmsg);
11561155
}
11571156
break;
11581157
}
@@ -3045,20 +3044,21 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
30453044
numberOfTemplateInstantiations = mTemplateInstantiations.size();
30463045
++recursiveCount;
30473046
if (recursiveCount > mSettings.maxTemplateRecursion) {
3048-
std::list<std::string> typeStringsUsedInTemplateInstantiation;
3049-
const std::string typeForNewName = templateDeclaration.name() + "<" + getNewName(instantiation.token(), typeStringsUsedInTemplateInstantiation) + ">";
3050-
3051-
const std::list<const Token *> callstack(1, instantiation.token());
3052-
const ErrorMessage errmsg(callstack,
3053-
&mTokenizer.list,
3054-
Severity::information,
3055-
"templateRecursion",
3056-
"TemplateSimplifier: max template recursion ("
3057-
+ MathLib::toString(mSettings.maxTemplateRecursion)
3058-
+ ") reached for template '"+typeForNewName+"'. You might want to limit Cppcheck recursion.",
3059-
Certainty::normal);
3060-
if (mErrorLogger && mSettings.severity.isEnabled(Severity::information))
3047+
if (mErrorLogger && mSettings.severity.isEnabled(Severity::information)) {
3048+
std::list<std::string> typeStringsUsedInTemplateInstantiation;
3049+
const std::string typeForNewName = templateDeclaration.name() + "<" + getNewName(instantiation.token(), typeStringsUsedInTemplateInstantiation) + ">";
3050+
3051+
const std::list<const Token *> callstack(1, instantiation.token());
3052+
const ErrorMessage errmsg(callstack,
3053+
&mTokenizer.list,
3054+
Severity::information,
3055+
"templateRecursion",
3056+
"TemplateSimplifier: max template recursion ("
3057+
+ MathLib::toString(mSettings.maxTemplateRecursion)
3058+
+ ") reached for template '"+typeForNewName+"'. You might want to limit Cppcheck recursion.",
3059+
Certainty::normal);
30613060
mErrorLogger->reportErr(errmsg);
3061+
}
30623062

30633063
// bail out..
30643064
break;
@@ -3896,15 +3896,14 @@ void TemplateSimplifier::simplifyTemplates(
38963896
}
38973897

38983898
if (passCount == passCountMax) {
3899-
if (mSettings.debugwarnings) {
3899+
if (mSettings.debugwarnings && mErrorLogger) {
39003900
const std::list<const Token*> locationList(1, mTokenList.front());
39013901
const ErrorMessage errmsg(locationList, &mTokenizer.list,
39023902
Severity::debug,
39033903
"debug",
39043904
"TemplateSimplifier: pass count limit hit before simplifications were finished.",
39053905
Certainty::normal);
3906-
if (mErrorLogger)
3907-
mErrorLogger->reportErr(errmsg);
3906+
mErrorLogger->reportErr(errmsg);
39083907
}
39093908
}
39103909

0 commit comments

Comments
 (0)