Skip to content

Commit 0baad49

Browse files
committed
Fixed #6383 (FP shiftNegative - value converted to unsigned in function argument)
1 parent 3082612 commit 0baad49

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

lib/checkother.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,11 @@ void CheckOther::redundantCopyError(const Token *tok,const std::string& varname)
21782178
// Checking for shift by negative values
21792179
//---------------------------------------------------------------------------
21802180

2181+
static bool isNegative(const Token *tok, const Settings *settings)
2182+
{
2183+
return tok->valueType() && tok->valueType()->sign == ValueType::SIGNED && tok->getValueLE(-1LL, settings);
2184+
}
2185+
21812186
void CheckOther::checkNegativeBitwiseShift()
21822187
{
21832188
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
@@ -2213,9 +2218,9 @@ void CheckOther::checkNegativeBitwiseShift()
22132218
continue;
22142219

22152220
// Get negative rhs value. preferably a value which doesn't have 'condition'.
2216-
if (tok->astOperand1()->getValueLE(-1LL, _settings))
2221+
if (isNegative(tok->astOperand1(), _settings))
22172222
negativeBitwiseShiftError(tok, 1);
2218-
else if (tok->astOperand2()->getValueLE(-1LL, _settings))
2223+
else if (isNegative(tok->astOperand2(), _settings))
22192224
negativeBitwiseShiftError(tok, 2);
22202225
}
22212226
}

test/testother.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,6 +4788,10 @@ class TestOther : public TestFixture {
47884788
// Negative LHS
47894789
check("const int x = -1 >> 2;");
47904790
ASSERT_EQUALS("[test.cpp:1]: (error) Shifting a negative value is undefined behaviour\n", errout.str());
4791+
4792+
// #6383 - unsigned type
4793+
check("const int x = (unsigned int)(-1) >> 2;");
4794+
ASSERT_EQUALS("", errout.str());
47914795
}
47924796

47934797
void incompleteArrayFill() {

0 commit comments

Comments
 (0)