Fixed #6383 (FP shiftNegative - value converted to unsigned in function argument)

This commit is contained in:
Daniel Marjamäki 2015-12-14 10:55:23 +01:00
parent 3082612e6d
commit 0baad496f2
2 changed files with 11 additions and 2 deletions

View File

@ -2178,6 +2178,11 @@ void CheckOther::redundantCopyError(const Token *tok,const std::string& varname)
// Checking for shift by negative values
//---------------------------------------------------------------------------
static bool isNegative(const Token *tok, const Settings *settings)
{
return tok->valueType() && tok->valueType()->sign == ValueType::SIGNED && tok->getValueLE(-1LL, settings);
}
void CheckOther::checkNegativeBitwiseShift()
{
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
@ -2213,9 +2218,9 @@ void CheckOther::checkNegativeBitwiseShift()
continue;
// Get negative rhs value. preferably a value which doesn't have 'condition'.
if (tok->astOperand1()->getValueLE(-1LL, _settings))
if (isNegative(tok->astOperand1(), _settings))
negativeBitwiseShiftError(tok, 1);
else if (tok->astOperand2()->getValueLE(-1LL, _settings))
else if (isNegative(tok->astOperand2(), _settings))
negativeBitwiseShiftError(tok, 2);
}
}

View File

@ -4788,6 +4788,10 @@ private:
// Negative LHS
check("const int x = -1 >> 2;");
ASSERT_EQUALS("[test.cpp:1]: (error) Shifting a negative value is undefined behaviour\n", errout.str());
// #6383 - unsigned type
check("const int x = (unsigned int)(-1) >> 2;");
ASSERT_EQUALS("", errout.str());
}
void incompleteArrayFill() {