From 0baad496f26a68154cf33d74157164a07db450e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 14 Dec 2015 10:55:23 +0100 Subject: [PATCH] Fixed #6383 (FP shiftNegative - value converted to unsigned in function argument) --- lib/checkother.cpp | 9 +++++++-- test/testother.cpp | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 23874e2af..6c9f85ef4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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); } } diff --git a/test/testother.cpp b/test/testother.cpp index 02d443457..e597e4744 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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() {