ErrorPath: clarify the 'shiftTooManyBits' error message

This commit is contained in:
Daniel Marjamäki 2017-05-22 07:58:56 +02:00
parent 882e1e6064
commit 9374055238
1 changed files with 11 additions and 15 deletions

View File

@ -44,9 +44,6 @@ static const struct CWE CWE190(190U); // Integer Overflow or Wraparound
void CheckType::checkTooBigBitwiseShift() void CheckType::checkTooBigBitwiseShift()
{ {
const bool printWarnings = _settings->isEnabled(Settings::WARNING);
const bool printInconclusive = _settings->inconclusive;
// unknown sizeof(int) => can't run this checker // unknown sizeof(int) => can't run this checker
if (_settings->platformType == Settings::Unspecified) if (_settings->platformType == Settings::Unspecified)
return; return;
@ -82,28 +79,27 @@ void CheckType::checkTooBigBitwiseShift()
// Get biggest rhs value. preferably a value which doesn't have 'condition'. // Get biggest rhs value. preferably a value which doesn't have 'condition'.
const ValueFlow::Value *value = tok->astOperand2()->getValueGE(lhsbits, _settings); const ValueFlow::Value *value = tok->astOperand2()->getValueGE(lhsbits, _settings);
if (!value) if (value && _settings->isEnabled(value, false))
continue; tooBigBitwiseShiftError(tok, lhsbits, *value);
if (value->condition && !printWarnings)
continue;
if (value->inconclusive && !printInconclusive)
continue;
tooBigBitwiseShiftError(tok, lhsbits, *value);
} }
} }
} }
void CheckType::tooBigBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits) void CheckType::tooBigBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits)
{ {
std::list<const Token*> callstack; if (!tok) {
callstack.push_back(tok); reportError(tok, Severity::error, "shiftTooManyBits", "Shifting 32-bit value by 40 bits is undefined behaviour", CWE758, false);
if (rhsbits.condition) return;
callstack.push_back(rhsbits.condition); }
const ErrorPath errorPath = getErrorPath(tok, &rhsbits, "Shift");
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "Shifting " << lhsbits << "-bit value by " << rhsbits.intvalue << " bits is undefined behaviour"; errmsg << "Shifting " << lhsbits << "-bit value by " << rhsbits.intvalue << " bits is undefined behaviour";
if (rhsbits.condition) if (rhsbits.condition)
errmsg << ". See condition at line " << rhsbits.condition->linenr() << "."; errmsg << ". See condition at line " << rhsbits.condition->linenr() << ".";
reportError(callstack, rhsbits.condition ? Severity::warning : Severity::error, "shiftTooManyBits", errmsg.str(), CWE758, rhsbits.inconclusive);
reportError(errorPath, rhsbits.condition ? Severity::warning : Severity::error, "shiftTooManyBits", errmsg.str(), CWE758, rhsbits.inconclusive);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------