Skip to content

Commit 5e10e68

Browse files
committed
CWE: refactoring. use constants instead of magic numbers.
1 parent cb6c9e1 commit 5e10e68

16 files changed

Lines changed: 198 additions & 156 deletions

lib/check.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
/// @addtogroup Core
3434
/// @{
3535

36+
struct CWE {
37+
explicit CWE(unsigned short ID) : id(ID) {}
38+
unsigned short id;
39+
};
40+
3641
/**
3742
* @brief Interface class that cppcheck uses to communicate with the checks.
3843
* All checking classes must inherit from this class
@@ -111,27 +116,27 @@ class CPPCHECKLIB Check {
111116
/** report an error */
112117
template<typename T, typename U>
113118
void reportError(const Token *tok, const Severity::SeverityType severity, const T id, const U msg) {
114-
reportError(tok, severity, id, msg, 0U, false);
119+
reportError(tok, severity, id, msg, CWE(0U), false);
115120
}
116121

117122
/** report an error */
118123
template<typename T, typename U>
119-
void reportError(const Token *tok, const Severity::SeverityType severity, const T id, const U msg, unsigned int cwe, bool inconclusive) {
124+
void reportError(const Token *tok, const Severity::SeverityType severity, const T id, const U msg, const CWE &cwe, bool inconclusive) {
120125
std::list<const Token *> callstack(1, tok);
121126
reportError(callstack, severity, id, msg, cwe, inconclusive);
122127
}
123128

124129
/** report an error */
125130
template<typename T, typename U>
126131
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const T id, const U msg) {
127-
reportError(callstack, severity, id, msg, 0U, false);
132+
reportError(callstack, severity, id, msg, CWE(0U), false);
128133
}
129134

130135
/** report an error */
131136
template<typename T, typename U>
132-
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const T id, const U msg, unsigned int cwe, bool inconclusive) {
137+
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const T id, const U msg, const CWE &cwe, bool inconclusive) {
133138
ErrorLogger::ErrorMessage errmsg(callstack, _tokenizer?&_tokenizer->list:0, severity, id, msg, inconclusive);
134-
errmsg._cwe = cwe;
139+
errmsg._cwe = cwe.id;
135140
if (_errorLogger)
136141
_errorLogger->reportErr(errmsg);
137142
else

lib/checkautovariables.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ namespace {
3333
static CheckAutoVariables instance;
3434
}
3535

36+
static const CWE CWE562(562U);
37+
static const CWE CWE590(590U);
3638

3739
bool CheckAutoVariables::isPtrArg(const Token *tok)
3840
{
@@ -287,12 +289,12 @@ void CheckAutoVariables::returnPointerToLocalArray()
287289

288290
void CheckAutoVariables::errorReturnAddressToAutoVariable(const Token *tok)
289291
{
290-
reportError(tok, Severity::error, "returnAddressOfAutoVariable", "Address of an auto-variable returned.", 562U, false);
292+
reportError(tok, Severity::error, "returnAddressOfAutoVariable", "Address of an auto-variable returned.", CWE562, false);
291293
}
292294

293295
void CheckAutoVariables::errorReturnPointerToLocalArray(const Token *tok)
294296
{
295-
reportError(tok, Severity::error, "returnLocalVariable", "Pointer to local array variable returned.", 562, false);
297+
reportError(tok, Severity::error, "returnLocalVariable", "Pointer to local array variable returned.", CWE562, false);
296298
}
297299

298300
void CheckAutoVariables::errorAutoVariableAssignment(const Token *tok, bool inconclusive)
@@ -303,15 +305,15 @@ void CheckAutoVariables::errorAutoVariableAssignment(const Token *tok, bool inco
303305
"Dangerous assignment - the function parameter is assigned the address of a local "
304306
"auto-variable. Local auto-variables are reserved from the stack which "
305307
"is freed when the function ends. So the pointer to a local variable "
306-
"is invalid after the function ends.", 562U, false);
308+
"is invalid after the function ends.", CWE562, false);
307309
} else {
308310
reportError(tok, Severity::error, "autoVariables",
309311
"Address of local auto-variable assigned to a function parameter.\n"
310312
"Function parameter is assigned the address of a local auto-variable. "
311313
"Local auto-variables are reserved from the stack which is freed when "
312314
"the function ends. The address is invalid after the function ends and it "
313315
"might 'leak' from the function through the parameter.",
314-
562U,
316+
CWE562,
315317
true);
316318
}
317319
}
@@ -322,7 +324,7 @@ void CheckAutoVariables::errorReturnAddressOfFunctionParameter(const Token *tok,
322324
"Address of function parameter '" + varname + "' returned.\n"
323325
"Address of the function parameter '" + varname + "' becomes invalid after the function exits because "
324326
"function parameters are stored on the stack which is freed when the function exits. Thus the returned "
325-
"value is invalid.", 562U, false);
327+
"value is invalid.", CWE562, false);
326328
}
327329

328330
void CheckAutoVariables::errorUselessAssignmentArg(const Token *tok)
@@ -492,12 +494,12 @@ void CheckAutoVariables::returnReference()
492494

493495
void CheckAutoVariables::errorReturnReference(const Token *tok)
494496
{
495-
reportError(tok, Severity::error, "returnReference", "Reference to auto variable returned.", 562U, false);
497+
reportError(tok, Severity::error, "returnReference", "Reference to auto variable returned.", CWE562, false);
496498
}
497499

498500
void CheckAutoVariables::errorReturnTempReference(const Token *tok)
499501
{
500-
reportError(tok, Severity::error, "returnTempReference", "Reference to temporary returned.", 562U, false);
502+
reportError(tok, Severity::error, "returnTempReference", "Reference to temporary returned.", CWE562, false);
501503
}
502504

503505
void CheckAutoVariables::errorInvalidDeallocation(const Token *tok)
@@ -507,5 +509,5 @@ void CheckAutoVariables::errorInvalidDeallocation(const Token *tok)
507509
"autovarInvalidDeallocation",
508510
"Deallocation of an auto-variable results in undefined behaviour.\n"
509511
"The deallocation of an auto-variable results in undefined behaviour. You should only free memory "
510-
"that has been allocated dynamically.", 590U, false);
512+
"that has been allocated dynamically.", CWE590, false);
511513
}

lib/checkbool.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace {
2828
CheckBool instance;
2929
}
3030

31+
static const CWE CWE571(571);
32+
static const CWE CWE587(587);
3133

3234
static bool astIsBool(const Token *expr)
3335
{
@@ -106,7 +108,7 @@ void CheckBool::bitwiseOnBooleanError(const Token *tok, const std::string &varna
106108
{
107109
reportError(tok, Severity::style, "bitwiseOnBoolean",
108110
"Boolean variable '" + varname + "' is used in bitwise operation. Did you mean '" + op + "'?",
109-
0U,
111+
CWE(0),
110112
true);
111113
}
112114

@@ -352,7 +354,7 @@ void CheckBool::checkAssignBoolToPointer()
352354
void CheckBool::assignBoolToPointerError(const Token *tok)
353355
{
354356
reportError(tok, Severity::error, "assignBoolToPointer",
355-
"Boolean value assigned to pointer.", 587U, false);
357+
"Boolean value assigned to pointer.", CWE587, false);
356358
}
357359

358360
//-----------------------------------------------------------------------------
@@ -473,7 +475,7 @@ void CheckBool::pointerArithBoolError(const Token *tok)
473475
Severity::error,
474476
"pointerArithBool",
475477
"Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour.\n"
476-
"Converting pointer arithmetic result to bool. The boolean result is always true unless there is pointer arithmetic overflow, and overflow is undefined behaviour. Probably a dereference is forgotten.", 571U, false);
478+
"Converting pointer arithmetic result to bool. The boolean result is always true unless there is pointer arithmetic overflow, and overflow is undefined behaviour. Probably a dereference is forgotten.", CWE571, false);
477479
}
478480

479481
void CheckBool::checkAssignBoolToFloat()

lib/checkboost.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace {
2424
CheckBoost instance;
2525
}
2626

27+
static const CWE CWE664(664);
28+
2729
void CheckBoost::checkBoostForeachModification()
2830
{
2931
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
@@ -55,6 +57,6 @@ void CheckBoost::checkBoostForeachModification()
5557
void CheckBoost::boostForeachError(const Token *tok)
5658
{
5759
reportError(tok, Severity::error, "boostForeachError",
58-
"BOOST_FOREACH caches the end() iterator. It's undefined behavior if you modify the container inside.", 664U, false
60+
"BOOST_FOREACH caches the end() iterator. It's undefined behavior if you modify the container inside.", CWE664, false
5961
);
6062
}

lib/checkbufferoverrun.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ namespace {
4343

4444
//---------------------------------------------------------------------------
4545

46+
// CWE ids used:
47+
static const CWE CWE119(119U);
48+
static const CWE CWE131(131U);
49+
static const CWE CWE788(788U);
50+
51+
//---------------------------------------------------------------------------
52+
4653
static void makeArrayIndexOutOfBoundsError(std::ostream& oss, const CheckBufferOverrun::ArrayInfo &arrayInfo, const std::vector<MathLib::bigint> &index)
4754
{
4855
oss << "Array '" << arrayInfo.varname();
@@ -61,7 +68,7 @@ void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const Arra
6168
{
6269
std::ostringstream oss;
6370
makeArrayIndexOutOfBoundsError(oss, arrayInfo, index);
64-
reportError(tok, Severity::error, "arrayIndexOutOfBounds", oss.str(), 788U, false);
71+
reportError(tok, Severity::error, "arrayIndexOutOfBounds", oss.str(), CWE788, false);
6572
}
6673

6774
void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const ArrayInfo &arrayInfo, const std::vector<ValueFlow::Value> &index)
@@ -92,7 +99,7 @@ void CheckBufferOverrun::arrayIndexOutOfBoundsError(const Token *tok, const Arra
9299
std::list<const Token *> callstack;
93100
callstack.push_back(tok);
94101
callstack.push_back(condition);
95-
reportError(callstack, Severity::warning, "arrayIndexOutOfBoundsCond", errmsg.str(), 0U, false);
102+
reportError(callstack, Severity::warning, "arrayIndexOutOfBoundsCond", errmsg.str(), CWE(0U), false);
96103
} else {
97104
std::ostringstream errmsg;
98105
errmsg << "Array '" << arrayInfo.varname();
@@ -133,7 +140,7 @@ static std::string bufferOverrunMessage(std::string varnames)
133140

134141
void CheckBufferOverrun::bufferOverrunError(const Token *tok, const std::string &varnames)
135142
{
136-
reportError(tok, Severity::error, "bufferAccessOutOfBounds", bufferOverrunMessage(varnames), 788U, false);
143+
reportError(tok, Severity::error, "bufferAccessOutOfBounds", bufferOverrunMessage(varnames), CWE788, false);
137144
}
138145

139146

@@ -177,7 +184,7 @@ void CheckBufferOverrun::outOfBoundsError(const Token *tok, const std::string &w
177184
if (show_size_info)
178185
oss << ": Supplied size " << supplied_size << " is larger than actual size " << actual_size;
179186
oss << '.';
180-
reportError(tok, Severity::error, "outOfBounds", oss.str(), 788U, false);
187+
reportError(tok, Severity::error, "outOfBounds", oss.str(), CWE788, false);
181188
}
182189

183190
void CheckBufferOverrun::pointerOutOfBoundsError(const Token *tok, const Token *index, const MathLib::bigint indexvalue)
@@ -221,12 +228,12 @@ void CheckBufferOverrun::terminateStrncpyError(const Token *tok, const std::stri
221228
"The buffer '" + varname + "' may not be null-terminated after the call to strncpy().\n"
222229
"If the source string's size fits or exceeds the given size, strncpy() does not add a "
223230
"zero at the end of the buffer. This causes bugs later in the code if the code "
224-
"assumes buffer is null-terminated.", 0U, true);
231+
"assumes buffer is null-terminated.", CWE(0U), true);
225232
}
226233

227234
void CheckBufferOverrun::cmdLineArgsError(const Token *tok)
228235
{
229-
reportError(tok, Severity::error, "insecureCmdLineArgs", "Buffer overrun possible for long command line arguments.", 119U, false);
236+
reportError(tok, Severity::error, "insecureCmdLineArgs", "Buffer overrun possible for long command line arguments.", CWE119, false);
230237
}
231238

232239
void CheckBufferOverrun::bufferNotZeroTerminatedError(const Token *tok, const std::string &varname, const std::string &function)
@@ -235,7 +242,7 @@ void CheckBufferOverrun::bufferNotZeroTerminatedError(const Token *tok, const st
235242
"The buffer '" + varname + "' is not null-terminated after the call to " + function + "(). "
236243
"This will cause bugs later in the code if the code assumes the buffer is null-terminated.";
237244

238-
reportError(tok, Severity::warning, "bufferNotZeroTerminated", errmsg, 0U, true);
245+
reportError(tok, Severity::warning, "bufferNotZeroTerminated", errmsg, CWE(0U), true);
239246
}
240247

241248
void CheckBufferOverrun::argumentSizeError(const Token *tok, const std::string &functionName, const std::string &varname)
@@ -248,7 +255,7 @@ void CheckBufferOverrun::negativeMemoryAllocationSizeError(const Token *tok)
248255
reportError(tok, Severity::error, "negativeMemoryAllocationSize",
249256
"Memory allocation size is negative.\n"
250257
"Memory allocation size is negative."
251-
"Negative allocation size has no specified behaviour.", 131U, false);
258+
"Negative allocation size has no specified behaviour.", CWE131, false);
252259
}
253260

254261
//---------------------------------------------------------------------------
@@ -1717,7 +1724,7 @@ void CheckBufferOverrun::negativeIndexError(const Token *tok, const ValueFlow::V
17171724
ostr << "Array index " << index.intvalue << " is out of bounds.";
17181725
if (index.condition)
17191726
ostr << " Otherwise there is useless condition at line " << index.condition->linenr() << ".";
1720-
reportError(tok, index.condition ? Severity::warning : Severity::error, "negativeIndex", ostr.str(), 0U, index.inconclusive);
1727+
reportError(tok, index.condition ? Severity::warning : Severity::error, "negativeIndex", ostr.str(), CWE(0U), index.inconclusive);
17211728
}
17221729

17231730
CheckBufferOverrun::ArrayInfo::ArrayInfo()

0 commit comments

Comments
 (0)