Skip to content

Commit 1db24ee

Browse files
committed
CWE mapping of signedCharArrayIndex, unknownSignCharArrayIndex, suspiciousCase, suspiciousEqualityComparison, duplicateBranch, duplicateExpressionTernary, suspiciousSemicolon, incompleteArrayFill, redundantPointerOp, unusedLabelSwitch, unusedLabel, unknownEvaluationOrder, stlIfFind, useAutoPointerCopy
1 parent ece4789 commit 1db24ee

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

lib/checkother.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ namespace {
3333
CheckOther instance;
3434
}
3535

36+
static const struct CWE CWE128(128U); // Wrap-around Error
37+
static const struct CWE CWE131(131U); // Incorrect Calculation of Buffer Size
3638
static const struct CWE CWE197(197U); // Numeric Truncation Error
3739
static const struct CWE CWE362(362U); // Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
3840
static const struct CWE CWE369(369U); // Divide By Zero
3941
static const struct CWE CWE398(398U); // Indicator of Poor Code Quality
4042
static const struct CWE CWE475(475U); // Undefined Behavior for Input to API
43+
static const struct CWE CWE482(482U); // Comparing instead of Assigning
4144
static const struct CWE CWE561(561U); // Dead Code
4245
static const struct CWE CWE563(563U); // Assignment to Variable without Use ('Unused Variable')
4346
static const struct CWE CWE570(570U); // Expression is Always False
@@ -47,6 +50,7 @@ static const struct CWE CWE687(687U); // Function Call With Incorrectly Specif
4750
static const struct CWE CWE688(688U); // Function Call With Incorrect Variable or Reference as Argument
4851
static const struct CWE CWE704(704U); // Incorrect Type Conversion or Cast
4952
static const struct CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
53+
static const struct CWE CWE768(768U); // Incorrect Short Circuit Evaluation
5054
static const struct CWE CWE783(783U); // Operator Precedence Logic Error
5155

5256
//----------------------------------------------------------------------------------
@@ -244,7 +248,7 @@ void CheckOther::checkSuspiciousSemicolon()
244248
void CheckOther::SuspiciousSemicolonError(const Token* tok)
245249
{
246250
reportError(tok, Severity::warning, "suspiciousSemicolon",
247-
"Suspicious use of ; at the end of '" + (tok ? tok->str() : std::string()) + "' statement.", CWE(0U), true);
251+
"Suspicious use of ; at the end of '" + (tok ? tok->str() : std::string()) + "' statement.", CWE398, true);
248252
}
249253

250254

@@ -893,7 +897,7 @@ void CheckOther::suspiciousCaseInSwitchError(const Token* tok, const std::string
893897
{
894898
reportError(tok, Severity::warning, "suspiciousCase",
895899
"Found suspicious case label in switch(). Operator '" + operatorString + "' probably doesn't work as intended.\n"
896-
"Using an operator like '" + operatorString + "' in a case label is suspicious. Did you intend to use a bitwise operator, multiple case labels or if/else instead?", CWE(0U), true);
900+
"Using an operator like '" + operatorString + "' in a case label is suspicious. Did you intend to use a bitwise operator, multiple case labels or if/else instead?", CWE398, true);
897901
}
898902

899903
//---------------------------------------------------------------------------
@@ -945,7 +949,7 @@ void CheckOther::checkSuspiciousEqualityComparison()
945949
void CheckOther::suspiciousEqualityComparisonError(const Token* tok)
946950
{
947951
reportError(tok, Severity::warning, "suspiciousEqualityComparison",
948-
"Found suspicious equality comparison. Did you intend to assign a value instead?", CWE(0U), true);
952+
"Found suspicious equality comparison. Did you intend to assign a value instead?", CWE482, true);
949953
}
950954

951955

@@ -1581,7 +1585,7 @@ void CheckOther::signedCharArrayIndexError(const Token *tok)
15811585
"Signed 'char' type used as array index.\n"
15821586
"Signed 'char' type used as array index. If the value "
15831587
"can be greater than 127 there will be a buffer underflow "
1584-
"because of sign extension.");
1588+
"because of sign extension.", CWE128, false);
15851589
}
15861590

15871591
void CheckOther::unknownSignCharArrayIndexError(const Token *tok)
@@ -1591,7 +1595,7 @@ void CheckOther::unknownSignCharArrayIndexError(const Token *tok)
15911595
"unknownSignCharArrayIndex",
15921596
"'char' type used as array index.\n"
15931597
"'char' type used as array index. Values greater that 127 will be "
1594-
"treated depending on whether 'char' is signed or unsigned on target platform.");
1598+
"treated depending on whether 'char' is signed or unsigned on target platform.", CWE758, false);
15951599
}
15961600

15971601
void CheckOther::charBitOpError(const Token *tok)
@@ -1856,7 +1860,7 @@ void CheckOther::duplicateBranchError(const Token *tok1, const Token *tok2)
18561860
reportError(toks, Severity::style, "duplicateBranch", "Found duplicate branches for 'if' and 'else'.\n"
18571861
"Finding the same code in an 'if' and related 'else' branch is suspicious and "
18581862
"might indicate a cut and paste or logic error. Please examine this code "
1859-
"carefully to determine if it is correct.", CWE(0U), true);
1863+
"carefully to determine if it is correct.", CWE398, true);
18601864
}
18611865

18621866

@@ -2057,7 +2061,7 @@ void CheckOther::duplicateExpressionTernaryError(const Token *tok)
20572061
{
20582062
reportError(tok, Severity::style, "duplicateExpressionTernary", "Same expression in both branches of ternary operator.\n"
20592063
"Finding the same expression in both branches of ternary operator is suspicious as "
2060-
"the same code is executed regardless of the condition.");
2064+
"the same code is executed regardless of the condition.", CWE398, false);
20612065
}
20622066

20632067
void CheckOther::selfAssignmentError(const Token *tok, const std::string &varname)
@@ -2381,11 +2385,11 @@ void CheckOther::incompleteArrayFillError(const Token* tok, const std::string& b
23812385
if (boolean)
23822386
reportError(tok, Severity::portability, "incompleteArrayFill",
23832387
"Array '" + buffer + "' might be filled incompletely. Did you forget to multiply the size given to '" + function + "()' with 'sizeof(*" + buffer + ")'?\n"
2384-
"The array '" + buffer + "' is filled incompletely. The function '" + function + "()' needs the size given in bytes, but the type 'bool' is larger than 1 on some platforms. Did you forget to multiply the size with 'sizeof(*" + buffer + ")'?", CWE(0U), true);
2388+
"The array '" + buffer + "' is filled incompletely. The function '" + function + "()' needs the size given in bytes, but the type 'bool' is larger than 1 on some platforms. Did you forget to multiply the size with 'sizeof(*" + buffer + ")'?", CWE131, true);
23852389
else
23862390
reportError(tok, Severity::warning, "incompleteArrayFill",
23872391
"Array '" + buffer + "' is filled incompletely. Did you forget to multiply the size given to '" + function + "()' with 'sizeof(*" + buffer + ")'?\n"
2388-
"The array '" + buffer + "' is filled incompletely. The function '" + function + "()' needs the size given in bytes, but an element of the given array is larger than one byte. Did you forget to multiply the size with 'sizeof(*" + buffer + ")'?", CWE(0U), true);
2392+
"The array '" + buffer + "' is filled incompletely. The function '" + function + "()' needs the size given in bytes, but an element of the given array is larger than one byte. Did you forget to multiply the size with 'sizeof(*" + buffer + ")'?", CWE131, true);
23892393
}
23902394

23912395
//---------------------------------------------------------------------------
@@ -2511,7 +2515,7 @@ void CheckOther::checkRedundantPointerOp()
25112515
void CheckOther::redundantPointerOpError(const Token* tok, const std::string &varname, bool inconclusive)
25122516
{
25132517
reportError(tok, Severity::style, "redundantPointerOp",
2514-
"Redundant pointer operation on " + varname + " - it's already a pointer.", CWE(0U), inconclusive);
2518+
"Redundant pointer operation on " + varname + " - it's already a pointer.", CWE398, inconclusive);
25152519
}
25162520

25172521
void CheckOther::checkInterlockedDecrement()
@@ -2585,11 +2589,11 @@ void CheckOther::unusedLabelError(const Token* tok, bool inSwitch)
25852589
if (inSwitch) {
25862590
if (!tok || _settings->isEnabled("warning"))
25872591
reportError(tok, Severity::warning, "unusedLabelSwitch",
2588-
"Label '" + (tok ? tok->str() : emptyString) + "' is not used. Should this be a 'case' of the enclosing switch()?");
2592+
"Label '" + (tok ? tok->str() : emptyString) + "' is not used. Should this be a 'case' of the enclosing switch()?", CWE398, false);
25892593
} else {
25902594
if (!tok || _settings->isEnabled("style"))
25912595
reportError(tok, Severity::style, "unusedLabel",
2592-
"Label '" + (tok ? tok->str() : emptyString) + "' is not used.");
2596+
"Label '" + (tok ? tok->str() : emptyString) + "' is not used.", CWE398, false);
25932597
}
25942598
}
25952599

@@ -2679,6 +2683,6 @@ void CheckOther::checkEvaluationOrder()
26792683
void CheckOther::unknownEvaluationOrder(const Token* tok)
26802684
{
26812685
reportError(tok, Severity::error, "unknownEvaluationOrder",
2682-
"Expression '" + (tok ? tok->expressionString() : std::string("x = x++;")) + "' depends on order of evaluation of side effects");
2686+
"Expression '" + (tok ? tok->expressionString() : std::string("x = x++;")) + "' depends on order of evaluation of side effects", CWE768, false);
26832687
}
26842688

lib/checkstl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ void CheckStl::if_findError(const Token *tok, bool str)
753753
"string. If your intention is to check that there are no findings in the string, "
754754
"you should compare with std::string::npos.", CWE597, false);
755755
else
756-
reportError(tok, Severity::warning, "stlIfFind", "Suspicious condition. The result of find() is an iterator, but it is not properly checked.");
756+
reportError(tok, Severity::warning, "stlIfFind", "Suspicious condition. The result of find() is an iterator, but it is not properly checked.", CWE398, false);
757757
}
758758

759759

@@ -1253,8 +1253,8 @@ void CheckStl::autoPointerError(const Token *tok)
12531253
{
12541254
reportError(tok, Severity::style, "useAutoPointerCopy",
12551255
"Copying 'auto_ptr' pointer to another does not create two equal objects since one has lost its ownership of the pointer.\n"
1256-
"'std::auto_ptr' has semantics of strict ownership, meaning that the 'auto_ptr' instance is the sole entity responsible for the object's lifetime. If an 'auto_ptr' is copied, the source looses the reference."
1257-
);
1256+
"'std::auto_ptr' has semantics of strict ownership, meaning that the 'auto_ptr' instance is the sole entity responsible for the object's lifetime. If an 'auto_ptr' is copied, the source looses the reference.",
1257+
CWE398, false);
12581258
}
12591259

12601260
void CheckStl::autoPointerContainerError(const Token *tok)

0 commit comments

Comments
 (0)