Skip to content

Commit 28f1222

Browse files
boosPKEuS
authored andcommitted
CWE mapping of useAutoPointerMalloc, uselessCallsCompare, uselessCallsSwap, uselessCallsSubstr, uselessCallsEmpty, uselessCallsRemove, derefInvalidIterator, reademptycontainer, multiplySizeof, divideSizeof, stringLiteralWrite, incorrectStringCompare, literalWithCharPtrCompare, charLiteralWithCharPtrCompare, incorrectStringBooleanError, staticStringCompare, stringCompare, signConversion, truncLongCastAssignment, truncLongCastReturn, unusedFunction, unusedVariable, unusedAllocatedMemory, unreadVariable, unassignedVariable, unusedStructMember, postfixOperator, va_start_wrongParameter (danmar#824)
Add an optional extended description…
1 parent 1484cee commit 28f1222

File tree

7 files changed

+34
-23
lines changed

7 files changed

+34
-23
lines changed

lib/checkpostfixoperator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ namespace {
3232
CheckPostfixOperator instance;
3333
}
3434

35+
36+
// CWE ids used
37+
static const struct CWE CWE398(398U); // Indicator of Poor Code Quality
38+
39+
3540
void CheckPostfixOperator::postfixOperator()
3641
{
3742
if (!_settings->isEnabled("performance"))
@@ -74,5 +79,5 @@ void CheckPostfixOperator::postfixOperatorError(const Token *tok)
7479
"Pre-increment/decrement can be more efficient than "
7580
"post-increment/decrement. Post-increment/decrement usually "
7681
"involves keeping a copy of the previous value around and "
77-
"adds a little extra code.");
82+
"adds a little extra code.", CWE398, false);
7883
}

lib/checksizeof.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,15 @@ void CheckSizeof::suspiciousSizeofCalculation()
338338
void CheckSizeof::multiplySizeofError(const Token *tok)
339339
{
340340
reportError(tok, Severity::warning,
341-
"multiplySizeof", "Multiplying sizeof() with sizeof() indicates a logic error.", CWE(0U), true);
341+
"multiplySizeof", "Multiplying sizeof() with sizeof() indicates a logic error.", CWE682, true);
342342
}
343343

344344
void CheckSizeof::divideSizeofError(const Token *tok)
345345
{
346346
reportError(tok, Severity::warning,
347347
"divideSizeof", "Division of result of sizeof() on pointer type.\n"
348348
"Division of result of sizeof() on pointer type. sizeof() returns the size of the pointer, "
349-
"not the size of the memory area it points to.", CWE(0U), true);
349+
"not the size of the memory area it points to.", CWE682, true);
350350
}
351351

352352
void CheckSizeof::sizeofVoid()

lib/checkstl.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ namespace {
3030
// CWE IDs used:
3131
static const struct CWE CWE398(398U); // Indicator of Poor Code Quality
3232
static const struct CWE CWE597(597U); // Use of Wrong Operator in String Comparison
33+
static const struct CWE CWE628(628U); // Function Call with Incorrectly Specified Arguments
3334
static const struct CWE CWE664(664U); // Improper Control of a Resource Through its Lifetime
3435
static const struct CWE CWE704(704U); // Incorrect Type Conversion or Cast
36+
static const struct CWE CWE762(762U); // Mismatched Memory Management Routines
3537
static const struct CWE CWE788(788U); // Access of Memory Location After End of Buffer
38+
static const struct CWE CWE825(825U); // Expired Pointer Dereference
3639
static const struct CWE CWE834(834U); // Excessive Iteration
3740

3841
// Error message for bad iterator usage..
@@ -1277,7 +1280,7 @@ void CheckStl::autoPointerMallocError(const Token *tok, const std::string& alloc
12771280
{
12781281
const std::string summary = "Object pointed by an 'auto_ptr' is destroyed using operator 'delete'. You should not use 'auto_ptr' for pointers obtained with function '" + allocFunction + "'.";
12791282
const std::string verbose = summary + " This means that you should only use 'auto_ptr' for pointers obtained with operator 'new'. This excludes use C library allocation functions (for example '" + allocFunction + "'), which must be deallocated by the appropriate C library function.";
1280-
reportError(tok, Severity::error, "useAutoPointerMalloc", summary + "\n" + verbose);
1283+
reportError(tok, Severity::error, "useAutoPointerMalloc", summary + "\n" + verbose, CWE762, false);
12811284
}
12821285

12831286
namespace {
@@ -1341,7 +1344,7 @@ void CheckStl::uselessCallsReturnValueError(const Token *tok, const std::string
13411344
<< "(" << varname << "." << function << "(" << varname << ")). As it is currently the "
13421345
<< "code is inefficient. It is possible either the string searched ('"
13431346
<< varname << "') or searched for ('" << varname << "') is wrong.";
1344-
reportError(tok, Severity::warning, "uselessCallsCompare", errmsg.str());
1347+
reportError(tok, Severity::warning, "uselessCallsCompare", errmsg.str(), CWE628, false);
13451348
}
13461349

13471350
void CheckStl::uselessCallsSwapError(const Token *tok, const std::string &varname)
@@ -1351,27 +1354,27 @@ void CheckStl::uselessCallsSwapError(const Token *tok, const std::string &varnam
13511354
<< "The 'swap()' function has no logical effect when given itself as parameter "
13521355
<< "(" << varname << ".swap(" << varname << ")). As it is currently the "
13531356
<< "code is inefficient. Is the object or the parameter wrong here?";
1354-
reportError(tok, Severity::performance, "uselessCallsSwap", errmsg.str());
1357+
reportError(tok, Severity::performance, "uselessCallsSwap", errmsg.str(), CWE628, false);
13551358
}
13561359

13571360
void CheckStl::uselessCallsSubstrError(const Token *tok, bool empty)
13581361
{
13591362
if (empty)
1360-
reportError(tok, Severity::performance, "uselessCallsSubstr", "Ineffective call of function 'substr' because it returns an empty string.");
1363+
reportError(tok, Severity::performance, "uselessCallsSubstr", "Ineffective call of function 'substr' because it returns an empty string.", CWE398, false);
13611364
else
1362-
reportError(tok, Severity::performance, "uselessCallsSubstr", "Ineffective call of function 'substr' because it returns a copy of the object. Use operator= instead.");
1365+
reportError(tok, Severity::performance, "uselessCallsSubstr", "Ineffective call of function 'substr' because it returns a copy of the object. Use operator= instead.", CWE398, false);
13631366
}
13641367

13651368
void CheckStl::uselessCallsEmptyError(const Token *tok)
13661369
{
1367-
reportError(tok, Severity::warning, "uselessCallsEmpty", "Ineffective call of function 'empty()'. Did you intend to call 'clear()' instead?");
1370+
reportError(tok, Severity::warning, "uselessCallsEmpty", "Ineffective call of function 'empty()'. Did you intend to call 'clear()' instead?", CWE398, false);
13681371
}
13691372

13701373
void CheckStl::uselessCallsRemoveError(const Token *tok, const std::string& function)
13711374
{
13721375
reportError(tok, Severity::warning, "uselessCallsRemove", "Return value of std::" + function + "() ignored. Elements remain in container.\n"
13731376
"The return value of std::" + function + "() is ignored. This function returns an iterator to the end of the range containing those elements that should be kept. "
1374-
"Elements past new end remain valid but with unspecified values. Use the erase method of the container to delete them.");
1377+
"Elements past new end remain valid but with unspecified values. Use the erase method of the container to delete them.", CWE762, false);
13751378
}
13761379

13771380
// Check for iterators being dereferenced before being checked for validity.
@@ -1442,7 +1445,7 @@ void CheckStl::dereferenceInvalidIteratorError(const Token* deref, const std::st
14421445
{
14431446
reportError(deref, Severity::warning,
14441447
"derefInvalidIterator", "Possible dereference of an invalid iterator: " + iterName + "\n" +
1445-
"Make sure to check that the iterator is valid before dereferencing it - not after.");
1448+
"Make sure to check that the iterator is valid before dereferencing it - not after.", CWE825, false);
14461449
}
14471450

14481451

@@ -1557,5 +1560,5 @@ void CheckStl::readingEmptyStlContainer()
15571560

15581561
void CheckStl::readingEmptyStlContainerError(const Token *tok)
15591562
{
1560-
reportError(tok, Severity::style, "reademptycontainer", "Reading from empty STL container '" + (tok ? tok->str() : std::string("var")) + "'", CWE(0U), true);
1563+
reportError(tok, Severity::style, "reademptycontainer", "Reading from empty STL container '" + (tok ? tok->str() : std::string("var")) + "'", CWE398, true);
15611564
}

lib/checkstring.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static const struct CWE CWE571(571U); // Expression is Always True
3434
static const struct CWE CWE595(595U); // Comparison of Object References Instead of Object Contents
3535
static const struct CWE CWE628(628U); // Function Call with Incorrectly Specified Arguments
3636
static const struct CWE CWE665(665U); // Improper Initialization
37-
37+
static const struct CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
3838

3939
//---------------------------------------------------------------------------
4040
// Writing string literal is UB
@@ -75,7 +75,7 @@ void CheckString::stringLiteralWriteError(const Token *tok, const Token *strValu
7575
}
7676
errmsg += " directly or indirectly is undefined behaviour.";
7777

78-
reportError(callstack, Severity::error, "stringLiteralWrite", errmsg);
78+
reportError(callstack, Severity::error, "stringLiteralWrite", errmsg, CWE758, false);
7979
}
8080

8181
//---------------------------------------------------------------------------

lib/checktype.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace {
3737

3838
// CWE ids used:
3939
static const struct CWE CWE195(195U); // Signed to Unsigned Conversion Error
40+
static const struct CWE CWE197(197U); // Numeric Truncation Error
4041
static const struct CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
4142
static const struct CWE CWE190(190U); // Integer Overflow or Wraparound
4243

@@ -290,7 +291,7 @@ void CheckType::longCastAssignError(const Token *tok)
290291
Severity::style,
291292
"truncLongCastAssignment",
292293
"int result is assigned to long variable. If the variable is long to avoid loss of information, then you have loss of information.\n"
293-
"int result is assigned to long variable. If the variable is long to avoid loss of information, then there is loss of information. To avoid loss of information you must cast a calculation operand to long, for example 'l = a * b;' => 'l = (long)a * b;'.");
294+
"int result is assigned to long variable. If the variable is long to avoid loss of information, then there is loss of information. To avoid loss of information you must cast a calculation operand to long, for example 'l = a * b;' => 'l = (long)a * b;'.", CWE197, false);
294295
}
295296

296297
void CheckType::longCastReturnError(const Token *tok)
@@ -299,5 +300,5 @@ void CheckType::longCastReturnError(const Token *tok)
299300
Severity::style,
300301
"truncLongCastReturn",
301302
"int result is returned as long value. If the return value is long to avoid loss of information, then you have loss of information.\n"
302-
"int result is returned as long value. If the return value is long to avoid loss of information, then there is loss of information. To avoid loss of information you must cast a calculation operand to long, for example 'return a*b;' => 'return (long)a*b'.");
303+
"int result is returned as long value. If the return value is long to avoid loss of information, then there is loss of information. To avoid loss of information you must cast a calculation operand to long, for example 'return a*b;' => 'return (long)a*b'.", CWE197, false);
303304
}

lib/checkunusedvar.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace {
3030
}
3131

3232
static const struct CWE CWE563(563U); // Assignment to Variable without Use ('Unused Variable')
33+
static const struct CWE CWE665(665U); // Improper Initialization
3334

3435

3536
/**
@@ -1205,17 +1206,17 @@ void CheckUnusedVar::unusedVariableError(const Token *tok, const std::string &va
12051206

12061207
void CheckUnusedVar::allocatedButUnusedVariableError(const Token *tok, const std::string &varname)
12071208
{
1208-
reportError(tok, Severity::style, "unusedAllocatedMemory", "Variable '" + varname + "' is allocated memory that is never used.");
1209+
reportError(tok, Severity::style, "unusedAllocatedMemory", "Variable '" + varname + "' is allocated memory that is never used.", CWE563, false);
12091210
}
12101211

12111212
void CheckUnusedVar::unreadVariableError(const Token *tok, const std::string &varname)
12121213
{
1213-
reportError(tok, Severity::style, "unreadVariable", "Variable '" + varname + "' is assigned a value that is never used.");
1214+
reportError(tok, Severity::style, "unreadVariable", "Variable '" + varname + "' is assigned a value that is never used.", CWE563, false);
12141215
}
12151216

12161217
void CheckUnusedVar::unassignedVariableError(const Token *tok, const std::string &varname)
12171218
{
1218-
reportError(tok, Severity::style, "unassignedVariable", "Variable '" + varname + "' is not assigned a value.");
1219+
reportError(tok, Severity::style, "unassignedVariable", "Variable '" + varname + "' is not assigned a value.", CWE665, false);
12191220
}
12201221

12211222
//---------------------------------------------------------------------------
@@ -1302,7 +1303,7 @@ void CheckUnusedVar::checkStructMemberUsage()
13021303
void CheckUnusedVar::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion)
13031304
{
13041305
const char* prefix = isUnion ? "union member '" : "struct member '";
1305-
reportError(tok, Severity::style, "unusedStructMember", std::string(prefix) + structname + "::" + varname + "' is never used.");
1306+
reportError(tok, Severity::style, "unusedStructMember", std::string(prefix) + structname + "::" + varname + "' is never used.", CWE563, false);
13061307
}
13071308

13081309
bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type)

lib/checkvaarg.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ namespace {
3232
//---------------------------------------------------------------------------
3333

3434
// CWE ids used:
35-
static const struct CWE CWE664(664U);
36-
static const struct CWE CWE758(758U);
35+
static const struct CWE CWE664(664U); // Improper Control of a Resource Through its Lifetime
36+
static const struct CWE CWE688(688U); // Function Call With Incorrect Variable or Reference as Argument
37+
static const struct CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
3738

3839
void CheckVaarg::va_start_argument()
3940
{
@@ -70,7 +71,7 @@ void CheckVaarg::va_start_argument()
7071
void CheckVaarg::wrongParameterTo_va_start_error(const Token *tok, const std::string& paramIsName, const std::string& paramShouldName)
7172
{
7273
reportError(tok, Severity::warning,
73-
"va_start_wrongParameter", "'" + paramIsName + "' given to va_start() is not last named argument of the function. Did you intend to pass '" + paramShouldName + "'?");
74+
"va_start_wrongParameter", "'" + paramIsName + "' given to va_start() is not last named argument of the function. Did you intend to pass '" + paramShouldName + "'?", CWE688, false);
7475
}
7576

7677
void CheckVaarg::referenceAs_va_start_error(const Token *tok, const std::string& paramName)

0 commit comments

Comments
 (0)