CheckNullPointer: Add missing id 'nullPointerArithmeticRedundantCheck' to errorlist (#1535)

* split CheckNullPointer::arithmeticError() into
  * CheckNullPointer::pointerArithmeticError() and
  * CheckNullPointer::redundantConditionWarning()

* Additional errorlist entry:

```XML
<error id="nullPointerArithmeticRedundantCheck"
  severity="warning"
  msg="Either the condition is redundant or there is pointer arithmetic with NULL pointer."
  verbose="Either the condition is redundant or there is pointer arithmetic with NULL pointer." cwe="682"/>
```
This commit is contained in:
ivangalkin 2018-12-29 21:34:22 +01:00 committed by Daniel Marjamäki
parent da91ce2016
commit 3f318548e2
2 changed files with 38 additions and 22 deletions

View File

@ -557,42 +557,56 @@ void CheckNullPointer::arithmetic()
continue;
if (value->condition && !mSettings->isEnabled(Settings::WARNING))
continue;
arithmeticError(tok,value);
if (value->condition)
redundantConditionWarning(tok, value, value->condition, value->isInconclusive());
else
pointerArithmeticError(tok, value, value->isInconclusive());
}
}
}
void CheckNullPointer::arithmeticError(const Token *tok, const ValueFlow::Value *value)
static std::string arithmeticTypeString(const Token *tok)
{
std::string arithmetic;
if (tok && tok->str()[0] == '-')
arithmetic = "subtraction";
return "subtraction";
else if (tok && tok->str()[0] == '+')
arithmetic = "addition";
return "addition";
else
arithmetic = "arithmetic";
return "arithmetic";
}
void CheckNullPointer::pointerArithmeticError(const Token* tok, const ValueFlow::Value *value, bool inconclusive) {
std::string arithmetic = arithmeticTypeString(tok);
std::string errmsg;
if (tok && tok->str()[0] == '-') {
if (value && value->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(value->condition) + " or there is overflow in pointer " + arithmetic + ".";
else
errmsg = "Overflow in pointer arithmetic, NULL pointer is subtracted.";
} else {
if (value && value->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(value->condition) + " or there is pointer arithmetic with NULL pointer.";
else
errmsg = "Pointer " + arithmetic + " with NULL pointer.";
}
const ErrorPath errorPath = getErrorPath(tok, value, "Null pointer " + arithmetic);
reportError(errorPath,
(value && value->condition) ? Severity::warning : Severity::error,
(value && value->condition) ? "nullPointerArithmeticRedundantCheck" : "nullPointerArithmetic",
Severity::error,
"nullPointerArithmetic",
errmsg,
CWE682, // unknown - pointer overflow
value && value->isInconclusive());
CWE682,
inconclusive);
}
void CheckNullPointer::redundantConditionWarning(const Token* tok, const ValueFlow::Value *value, const Token *condition, bool inconclusive) {
std::string arithmetic = arithmeticTypeString(tok);
std::string errmsg;
if (tok && tok->str()[0] == '-') {
errmsg = ValueFlow::eitherTheConditionIsRedundant(condition) + " or there is overflow in pointer " + arithmetic + ".";
} else {
errmsg = ValueFlow::eitherTheConditionIsRedundant(condition) + " or there is pointer arithmetic with NULL pointer.";
}
const ErrorPath errorPath = getErrorPath(tok, value, "Null pointer " + arithmetic);
reportError(errorPath,
Severity::warning,
"nullPointerArithmeticRedundantCheck",
errmsg,
CWE682,
inconclusive);
}
std::string CheckNullPointer::MyFileInfo::toString() const

View File

@ -126,7 +126,8 @@ private:
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
CheckNullPointer c(nullptr, settings, errorLogger);
c.nullPointerError(nullptr, "pointer", nullptr, false);
c.arithmeticError(nullptr, nullptr);
c.pointerArithmeticError(nullptr, nullptr, false);
c.redundantConditionWarning(nullptr, nullptr, nullptr, false);
}
/** Name of check */
@ -155,7 +156,8 @@ private:
/** undefined null pointer arithmetic */
void arithmetic();
void arithmeticError(const Token *tok, const ValueFlow::Value *value);
void pointerArithmeticError(const Token* tok, const ValueFlow::Value *value, bool inconclusive);
void redundantConditionWarning(const Token* tok, const ValueFlow::Value *value, const Token *condition, bool inconclusive);
};
/// @}
//---------------------------------------------------------------------------