Skip to content

Commit ec00824

Browse files
committed
- Print "inconclusive" tag in cli - Fixed inconclusive handling in checkbufferoverrun.cpp - Merged reportInconclusiveError into reportError by adding an additional parameter "bool inconclusive" which is false per default
1 parent 6ef92c4 commit ec00824

14 files changed

Lines changed: 298 additions & 332 deletions

lib/check.h

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -114,47 +114,25 @@ class Check {
114114
ErrorLogger * const _errorLogger;
115115

116116
/** report an error */
117-
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg) {
118-
std::list<const Token *> callstack;
119-
if (tok)
120-
callstack.push_back(tok);
121-
reportError(callstack, severity, id, msg);
117+
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg, bool inconclusive = false) {
118+
std::list<const Token *> callstack(1, tok);
119+
reportError(callstack, severity, id, msg, inconclusive);
122120
}
123121

124122
/** report an error */
125-
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string& msg) {
126-
reportError(callstack, severity, id, msg, false);
127-
}
128-
129-
/** report an inconclusive error */
130-
void reportInconclusiveError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg) {
131-
std::list<const Token *> callstack;
132-
if (tok)
133-
callstack.push_back(tok);
134-
reportInconclusiveError(callstack, severity, id, msg);
135-
}
136-
137-
/** report an inconclusive error */
138-
void reportInconclusiveError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string& msg) {
139-
reportError(callstack, severity, id, msg, true);
140-
}
141-
142-
143-
private:
144-
const std::string _name;
145-
146-
/** disabled assignment operator */
147-
void operator=(const Check &);
148-
149-
/** report an error */
150-
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string& msg, bool inconclusive) {
123+
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string& msg, bool inconclusive = false) {
151124
ErrorLogger::ErrorMessage errmsg(callstack, _tokenizer?&_tokenizer->list:0, severity, id, msg, inconclusive);
152125
if (_errorLogger)
153126
_errorLogger->reportErr(errmsg);
154127
else
155128
reportError(errmsg);
156129
}
157130

131+
private:
132+
const std::string _name;
133+
134+
/** disabled assignment operator */
135+
void operator=(const Check &);
158136
};
159137

160138
namespace std {

lib/checkautovariables.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ void CheckAutoVariables::errorAutoVariableAssignment(const Token *tok, bool inco
208208
"stack is freed when the function ends. So the pointer to a local variable "
209209
"is invalid after the function ends.");
210210
} else {
211-
reportInconclusiveError(tok, Severity::error, "autoVariables",
212-
"Inconclusive: Assigning address of local auto-variable to a function parameter.\n"
213-
"Inconclusive: function parameter takes the address of a local auto-variable. "
214-
"Local auto-variables are reserved from the stack. And the stack is freed when "
215-
"the function ends. The address is invalid after the function ends and it "
216-
"might 'leak' from the function through the parameter.");
211+
reportError(tok, Severity::error, "autoVariables",
212+
"Assigning address of local auto-variable to a function parameter.\n"
213+
"Function parameter takes the address of a local auto-variable. "
214+
"Local auto-variables are reserved from the stack. And the stack is freed when "
215+
"the function ends. The address is invalid after the function ends and it "
216+
"might 'leak' from the function through the parameter.", true);
217217
}
218218
}
219219

lib/checkbufferoverrun.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void CheckBufferOverrun::possibleReadlinkBufferOverrunError(const Token* tok, co
112112
funcname + "() might return the full size of '" + varname + "'. Lower the supplied size by one. "
113113
"If a " + varname + "[len] = '\\0'; statement follows, it will overrun the buffer.";
114114

115-
reportInconclusiveError(tok, Severity::warning, "possibleReadlinkBufferOverrun", errmsg);
115+
reportError(tok, Severity::warning, "possibleReadlinkBufferOverrun", errmsg, true);
116116
}
117117

118118
void CheckBufferOverrun::strncatUsageError(const Token *tok)
@@ -172,7 +172,7 @@ void CheckBufferOverrun::bufferNotZeroTerminatedError(const Token *tok, const st
172172
"The buffer '" + varname + "' is not zero-terminated after the call to " + function + "(). "
173173
"This will cause bugs later in the code if the code assumes the buffer is zero-terminated.";
174174

175-
reportInconclusiveError(tok, Severity::warning, "bufferNotZeroTerminated", errmsg);
175+
reportError(tok, Severity::warning, "bufferNotZeroTerminated", errmsg, true);
176176
}
177177

178178
//---------------------------------------------------------------------------

lib/checkclass.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,27 +1425,27 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func)
14251425

14261426
void CheckClass::checkConstError(const Token *tok, const std::string &classname, const std::string &funcname)
14271427
{
1428-
reportInconclusiveError(tok, Severity::style, "functionConst",
1429-
"Technically the member function '" + classname + "::" + funcname + "' can be const.\n"
1430-
"The member function '" + classname + "::" + funcname + "' can be made a const "
1431-
"function. Making this function const function should not cause compiler errors. "
1432-
"Even though the function can be made const function technically it may not make "
1433-
"sense conceptually. Think about your design and task of the function first - is "
1434-
"it a function that must not change object internal state?");
1428+
reportError(tok, Severity::style, "functionConst",
1429+
"Technically the member function '" + classname + "::" + funcname + "' can be const.\n"
1430+
"The member function '" + classname + "::" + funcname + "' can be made a const "
1431+
"function. Making this function const function should not cause compiler errors. "
1432+
"Even though the function can be made const function technically it may not make "
1433+
"sense conceptually. Think about your design and task of the function first - is "
1434+
"it a function that must not change object internal state?", true);
14351435
}
14361436

14371437
void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname)
14381438
{
14391439
std::list<const Token *> toks;
14401440
toks.push_back(tok1);
14411441
toks.push_back(tok2);
1442-
reportInconclusiveError(toks, Severity::style, "functionConst",
1443-
"Technically the member function '" + classname + "::" + funcname + "' can be const.\n"
1444-
"The member function '" + classname + "::" + funcname + "' can be made a const "
1445-
"function. Making this function const function should not cause compiler errors. "
1446-
"Even though the function can be made const function technically it may not make "
1447-
"sense conceptually. Think about your design and task of the function first - is "
1448-
"it a function that must not change object internal state?");
1442+
reportError(toks, Severity::style, "functionConst",
1443+
"Technically the member function '" + classname + "::" + funcname + "' can be const.\n"
1444+
"The member function '" + classname + "::" + funcname + "' can be made a const "
1445+
"function. Making this function const function should not cause compiler errors. "
1446+
"Even though the function can be made const function technically it may not make "
1447+
"sense conceptually. Think about your design and task of the function first - is "
1448+
"it a function that must not change object internal state?", true);
14491449
}
14501450

14511451
//---------------------------------------------------------------------------
@@ -1527,11 +1527,11 @@ void CheckClass::initializerListError(const Token *tok1, const Token *tok2, cons
15271527
std::list<const Token *> toks;
15281528
toks.push_back(tok1);
15291529
toks.push_back(tok2);
1530-
reportInconclusiveError(toks, Severity::style, "initializerList",
1531-
"Member variable '" + classname + "::" +
1532-
varname + "' is in the wrong order in the initializer list.\n"
1533-
"Members are initialized in the order they are declared, not the "
1534-
"order they are in the initializer list. Keeping the initializer list "
1535-
"in the same order that the members were declared prevents order dependent "
1536-
"initialization errors.");
1530+
reportError(toks, Severity::style, "initializerList",
1531+
"Member variable '" + classname + "::" +
1532+
varname + "' is in the wrong order in the initializer list.\n"
1533+
"Members are initialized in the order they are declared, not the "
1534+
"order they are in the initializer list. Keeping the initializer list "
1535+
"in the same order that the members were declared prevents order dependent "
1536+
"initialization errors.", true);
15371537
}

lib/checknullpointer.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,8 +1421,5 @@ void CheckNullPointer::nullPointerError(const Token *tok, const std::string &var
14211421
void CheckNullPointer::nullPointerError(const Token *tok, const std::string &varname, const unsigned int line, bool inconclusive)
14221422
{
14231423
const std::string errmsg("Possible null pointer dereference: " + varname + " - otherwise it is redundant to check if " + varname + " is null at line " + MathLib::toString<unsigned int>(line));
1424-
if (inconclusive)
1425-
reportInconclusiveError(tok, Severity::error, "nullPointer", errmsg);
1426-
else
1427-
reportError(tok, Severity::error, "nullPointer", errmsg);
1424+
reportError(tok, Severity::error, "nullPointer", errmsg, inconclusive);
14281425
}

lib/checkother.cpp

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ void CheckOther::checkBitwiseOnBoolean()
259259

260260
void CheckOther::bitwiseOnBooleanError(const Token *tok, const std::string &varname, const std::string &op)
261261
{
262-
reportInconclusiveError(tok, Severity::style, "bitwiseOnBoolean",
263-
"Boolean variable '" + varname + "' is used in bitwise operation. Did you mean " + op + " ?");
262+
reportError(tok, Severity::style, "bitwiseOnBoolean",
263+
"Boolean variable '" + varname + "' is used in bitwise operation. Did you mean " + op + " ?", true);
264264
}
265265

266266
void CheckOther::checkSuspiciousSemicolon()
@@ -291,8 +291,8 @@ void CheckOther::checkSuspiciousSemicolon()
291291

292292
void CheckOther::SuspiciousSemicolonError(const Token* tok)
293293
{
294-
reportInconclusiveError(tok, Severity::warning, "suspiciousSemicolon",
295-
"Suspicious use of ; at the end of 'if/for/while' statement.");
294+
reportError(tok, Severity::warning, "suspiciousSemicolon",
295+
"Suspicious use of ; at the end of 'if/for/while' statement.", true);
296296
}
297297

298298

@@ -418,7 +418,7 @@ void CheckOther::invalidPointerCastError(const Token* tok, const std::string& fr
418418
if (!inconclusive)
419419
reportError(tok, Severity::portability, "invalidPointerCast", "Casting from " + from + "* to integer* is not portable due to different binary data representations on different platforms");
420420
else
421-
reportInconclusiveError(tok, Severity::portability, "invalidPointerCast", "Casting from " + from + "* to char* might be not portable due to different binary data representations on different platforms");
421+
reportError(tok, Severity::portability, "invalidPointerCast", "Casting from " + from + "* to char* might be not portable due to different binary data representations on different platforms", true);
422422
} else
423423
reportError(tok, Severity::warning, "invalidPointerCast", "Casting between " + from + "* and " + to + "* which have an incompatible binary data representation");
424424
}
@@ -613,11 +613,11 @@ void CheckOther::checkSizeofForPointerSize()
613613

614614
void CheckOther::sizeofForPointerError(const Token *tok, const std::string &varname)
615615
{
616-
reportInconclusiveError(tok, Severity::warning, "pointerSize",
617-
"Using size of pointer " + varname + " instead of size of its data.\n"
618-
"Using size of pointer " + varname + " instead of size of its data. "
619-
"This is likely to lead to a buffer overflow. You probably intend to "
620-
"write sizeof(*" + varname + ")");
616+
reportError(tok, Severity::warning, "pointerSize",
617+
"Using size of pointer " + varname + " instead of size of its data.\n"
618+
"Using size of pointer " + varname + " instead of size of its data. "
619+
"This is likely to lead to a buffer overflow. You probably intend to "
620+
"write sizeof(*" + varname + ")", true);
621621
}
622622

623623
//---------------------------------------------------------------------------
@@ -1810,24 +1810,15 @@ void CheckOther::checkUnreachableCode()
18101810

18111811
void CheckOther::duplicateBreakError(const Token *tok, bool inconclusive)
18121812
{
1813-
if (inconclusive)
1814-
reportInconclusiveError(tok, Severity::style, "duplicateBreak",
1815-
"Consecutive return, break, continue, goto or throw statements are unnecessary.\n"
1816-
"The second of the two statements can never be executed, and so should be removed.");
1817-
else
1818-
reportError(tok, Severity::style, "duplicateBreak",
1819-
"Consecutive return, break, continue, goto or throw statements are unnecessary.\n"
1820-
"The second of the two statements can never be executed, and so should be removed.");
1813+
reportError(tok, Severity::style, "duplicateBreak",
1814+
"Consecutive return, break, continue, goto or throw statements are unnecessary.\n"
1815+
"The second of the two statements can never be executed, and so should be removed.", inconclusive);
18211816
}
18221817

18231818
void CheckOther::unreachableCodeError(const Token *tok, bool inconclusive)
18241819
{
1825-
if (inconclusive)
1826-
reportInconclusiveError(tok, Severity::style, "unreachableCode",
1827-
"Statements following return, break, continue, goto or throw will never be executed.");
1828-
else
1829-
reportError(tok, Severity::style, "unreachableCode",
1830-
"Statements following return, break, continue, goto or throw will never be executed.");
1820+
reportError(tok, Severity::style, "unreachableCode",
1821+
"Statements following return, break, continue, goto or throw will never be executed.", inconclusive);
18311822
}
18321823

18331824
//---------------------------------------------------------------------------
@@ -1877,7 +1868,7 @@ void CheckOther::checkUnsignedDivision()
18771868
void CheckOther::udivError(const Token *tok, bool inconclusive)
18781869
{
18791870
if (inconclusive)
1880-
reportInconclusiveError(tok, Severity::warning, "udivError", "Division with signed and unsigned operators. The result might be wrong.");
1871+
reportError(tok, Severity::warning, "udivError", "Division with signed and unsigned operators. The result might be wrong.", true);
18811872
else
18821873
reportError(tok, Severity::error, "udivError", "Unsigned division. The result will be wrong.");
18831874
}
@@ -3206,12 +3197,8 @@ void CheckOther::sizeofCalculation()
32063197

32073198
void CheckOther::sizeofCalculationError(const Token *tok, bool inconclusive)
32083199
{
3209-
if (inconclusive)
3210-
reportInconclusiveError(tok, Severity::warning,
3211-
"sizeofCalculation", "Found calculation inside sizeof()");
3212-
else
3213-
reportError(tok, Severity::warning,
3214-
"sizeofCalculation", "Found calculation inside sizeof()");
3200+
reportError(tok, Severity::warning,
3201+
"sizeofCalculation", "Found calculation inside sizeof()", inconclusive);
32153202
}
32163203

32173204
//-----------------------------------------------------------------------------
@@ -3342,12 +3329,12 @@ void CheckOther::checkSignOfUnsignedVariable()
33423329
void CheckOther::unsignedLessThanZeroError(const Token *tok, const std::string &varname, bool inconclusive)
33433330
{
33443331
if (inconclusive) {
3345-
reportInconclusiveError(tok, Severity::style, "unsignedLessThanZero",
3346-
"Checking if unsigned variable '" + varname + "' is less than zero. This might be a false warning.\n"
3347-
"Checking if unsigned variable '" + varname + "' is less than zero. An unsigned "
3348-
"variable will never be negative so it is either pointless or an error to check if it is. "
3349-
"It's not known if the used constant is a template parameter or not and therefore "
3350-
"this message might be a false warning");
3332+
reportError(tok, Severity::style, "unsignedLessThanZero",
3333+
"Checking if unsigned variable '" + varname + "' is less than zero. This might be a false warning.\n"
3334+
"Checking if unsigned variable '" + varname + "' is less than zero. An unsigned "
3335+
"variable will never be negative so it is either pointless or an error to check if it is. "
3336+
"It's not known if the used constant is a template parameter or not and therefore "
3337+
"this message might be a false warning", true);
33513338
} else {
33523339
reportError(tok, Severity::style, "unsignedLessThanZero",
33533340
"Checking if unsigned variable '" + varname + "' is less than zero.\n"
@@ -3359,11 +3346,11 @@ void CheckOther::unsignedLessThanZeroError(const Token *tok, const std::string &
33593346
void CheckOther::unsignedPositiveError(const Token *tok, const std::string &varname, bool inconclusive)
33603347
{
33613348
if (inconclusive) {
3362-
reportInconclusiveError(tok, Severity::style, "unsignedPositive",
3363-
"An unsigned variable '" + varname + "' can't be negative so it is unnecessary to test it. This might be a false warning.\n"
3364-
"An unsigned variable '" + varname + "' can't be negative so it is unnecessary to test it. "
3365-
"It's not known if the used constant is a "
3366-
"template parameter or not and therefore this message might be a false warning");
3349+
reportError(tok, Severity::style, "unsignedPositive",
3350+
"An unsigned variable '" + varname + "' can't be negative so it is unnecessary to test it. This might be a false warning.\n"
3351+
"An unsigned variable '" + varname + "' can't be negative so it is unnecessary to test it. "
3352+
"It's not known if the used constant is a "
3353+
"template parameter or not and therefore this message might be a false warning", true);
33673354
} else {
33683355
reportError(tok, Severity::style, "unsignedPositive",
33693356
"An unsigned variable '" + varname + "' can't be negative so it is unnecessary to test it.");

lib/errorlogger.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,12 @@ std::string ErrorLogger::ErrorMessage::toString(bool verbose, const std::string
285285
std::ostringstream text;
286286
if (!_callStack.empty())
287287
text << callStackToString(_callStack) << ": ";
288-
if (_severity != Severity::none)
289-
text << '(' << Severity::toString(_severity) << ") ";
288+
if (_severity != Severity::none) {
289+
text << '(' << Severity::toString(_severity);
290+
if (_inconclusive)
291+
text << ", inconclusive";
292+
text << ") ";
293+
}
290294
text << (verbose ? _verboseMessage : _shortMessage);
291295
return text.str();
292296
}

0 commit comments

Comments
 (0)