Skip to content

Commit 41dee04

Browse files
committed
CWE mapping of assignBoolToFloat, strncatUsage, sizeArgumentAsChar, terminateStrncpy, bufferNotZeroTerminated, negativeArraySize, noExplicitConstructor, virtualDestructor
1 parent d220573 commit 41dee04

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

lib/checkbool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace {
3232
static const CWE CWE398(398U); // Indicator of Poor Code Quality
3333
static const CWE CWE571(571U); // Expression is Always True
3434
static const CWE CWE587(587U); // Assignment of a Fixed Address to a Pointer
35+
static const CWE CWE704(704U); // Incorrect Type Conversion or Cast
3536

3637
//---------------------------------------------------------------------------
3738
//---------------------------------------------------------------------------
@@ -461,5 +462,5 @@ void CheckBool::checkAssignBoolToFloat()
461462
void CheckBool::assignBoolToFloatError(const Token *tok)
462463
{
463464
reportError(tok, Severity::style, "assignBoolToFloat",
464-
"Boolean value assigned to floating point variable.");
465+
"Boolean value assigned to floating point variable.", CWE704, false);
465466
}

lib/checkbufferoverrun.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ namespace {
4444
//---------------------------------------------------------------------------
4545

4646
// CWE ids used:
47-
static const CWE CWE131(131U);
48-
static const CWE CWE398(398U);
49-
static const CWE CWE786(786U);
50-
static const CWE CWE788(788U);
47+
static const CWE CWE131(131U); // Incorrect Calculation of Buffer Size
48+
static const CWE CWE170(170U); // Improper Null Termination
49+
static const CWE CWE398(398U); // Indicator of Poor Code Quality
50+
static const CWE CWE682(682U); // Incorrect Calculation
51+
static const CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
52+
static const CWE CWE786(786U); // Access of Memory Location Before Start of Buffer
53+
static const CWE CWE788(788U); // Access of Memory Location After End of Buffer
5154

5255
//---------------------------------------------------------------------------
5356

@@ -174,7 +177,7 @@ void CheckBufferOverrun::strncatUsageError(const Token *tok)
174177
"At most, strncat appends the 3rd parameter's amount of characters and adds a terminating null byte.\n"
175178
"The safe way to use strncat is to subtract one from the remaining space in the buffer and use it as 3rd parameter."
176179
"Source: http://www.cplusplus.com/reference/cstring/strncat/\n"
177-
"Source: http://www.opensource.apple.com/source/Libc/Libc-167/gen.subproj/i386.subproj/strncat.c");
180+
"Source: http://www.opensource.apple.com/source/Libc/Libc-167/gen.subproj/i386.subproj/strncat.c", CWE119, false);
178181
}
179182

180183
void CheckBufferOverrun::outOfBoundsError(const Token *tok, const std::string &what, const bool show_size_info, const MathLib::bigint &supplied_size, const MathLib::bigint &actual_size)
@@ -219,7 +222,7 @@ void CheckBufferOverrun::sizeArgumentAsCharError(const Token *tok)
219222
{
220223
if (_settings && !_settings->isEnabled("warning"))
221224
return;
222-
reportError(tok, Severity::warning, "sizeArgumentAsChar", "The size argument is given as a char constant.");
225+
reportError(tok, Severity::warning, "sizeArgumentAsChar", "The size argument is given as a char constant.", CWE682, false);
223226
}
224227

225228

@@ -229,7 +232,7 @@ void CheckBufferOverrun::terminateStrncpyError(const Token *tok, const std::stri
229232
"The buffer '" + varname + "' may not be null-terminated after the call to strncpy().\n"
230233
"If the source string's size fits or exceeds the given size, strncpy() does not add a "
231234
"zero at the end of the buffer. This causes bugs later in the code if the code "
232-
"assumes buffer is null-terminated.", CWE(0U), true);
235+
"assumes buffer is null-terminated.", CWE170, true);
233236
}
234237

235238
void CheckBufferOverrun::cmdLineArgsError(const Token *tok)
@@ -243,7 +246,7 @@ void CheckBufferOverrun::bufferNotZeroTerminatedError(const Token *tok, const st
243246
"The buffer '" + varname + "' is not null-terminated after the call to " + function + "(). "
244247
"This will cause bugs later in the code if the code assumes the buffer is null-terminated.";
245248

246-
reportError(tok, Severity::warning, "bufferNotZeroTerminated", errmsg, CWE(0U), true);
249+
reportError(tok, Severity::warning, "bufferNotZeroTerminated", errmsg, CWE170, true);
247250
}
248251

249252
void CheckBufferOverrun::argumentSizeError(const Token *tok, const std::string &functionName, const std::string &varname)
@@ -1084,7 +1087,7 @@ void CheckBufferOverrun::negativeArraySize()
10841087
void CheckBufferOverrun::negativeArraySizeError(const Token *tok)
10851088
{
10861089
reportError(tok, Severity::error, "negativeArraySize",
1087-
"Declaration of array '" + (tok ? tok->str() : std::string()) + "' with negative size is undefined behaviour");
1090+
"Declaration of array '" + (tok ? tok->str() : std::string()) + "' with negative size is undefined behaviour", CWE758, false);
10881091
}
10891092

10901093
//---------------------------------------------------------------------------

lib/checkclass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ void CheckClass::noExplicitConstructorError(const Token *tok, const std::string
802802
{
803803
const std::string message(std::string(isStruct ? "Struct" : "Class") + " '" + classname + "' has a constructor with 1 argument that is not explicit.");
804804
const std::string verbose(message + " Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.");
805-
reportError(tok, Severity::style, "noExplicitConstructor", message + "\n" + verbose);
805+
reportError(tok, Severity::style, "noExplicitConstructor", message + "\n" + verbose, CWE398, false);
806806
}
807807

808808
void CheckClass::uninitVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive)
@@ -1642,7 +1642,7 @@ void CheckClass::virtualDestructorError(const Token *tok, const std::string &Bas
16421642
"Class '" + Base + "' which is inherited by class '" + Derived + "' does not have a virtual destructor. "
16431643
"If you destroy instances of the derived class by deleting a pointer that points to the base class, only "
16441644
"the destructor of the base class is executed. Thus, dynamic memory that is managed by the derived class "
1645-
"could leak. This can be avoided by adding a virtual destructor to the base class.");
1645+
"could leak. This can be avoided by adding a virtual destructor to the base class.", CWE404, false);
16461646
}
16471647

16481648
//---------------------------------------------------------------------------

0 commit comments

Comments
 (0)