diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index a6b60e2b5..f0e49ca5b 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -432,7 +432,7 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti result.intvalue = value1->intvalue > value2->intvalue; else if (parent->str() == ">=") result.intvalue = value1->intvalue >= value2->intvalue; - else if (parent->str() == ">>") + else if (parent->str() == ">>" && value1->intvalue >= 0 && value2->intvalue >= 0) result.intvalue = value1->intvalue >> value2->intvalue; else break; @@ -445,7 +445,7 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti result.intvalue = value1->intvalue < value2->intvalue; else if (parent->str() == "<=") result.intvalue = value1->intvalue <= value2->intvalue; - else if (parent->str() == "<<") + else if (parent->str() == "<<" && value1->intvalue >= 0 && value2->intvalue >= 0) result.intvalue = value1->intvalue << value2->intvalue; else break; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 15cdd4411..589cc96eb 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -279,6 +279,12 @@ private: ASSERT_EQUALS(0, valueOfTok("3 < (a ? b : 2);", "<").intvalue); ASSERT_EQUALS(0, valueOfTok("3 <= (a ? b : 2);", "<=").intvalue); + // Don't calculate if there is UB + ASSERT(tokenValues("-1<<10","<<").empty()); + ASSERT(tokenValues("10<<-1","<<").empty()); + ASSERT(tokenValues("-1>>10",">>").empty()); + ASSERT(tokenValues("10>>-1",">>").empty()); + // calculation using 1,2 variables/values code = "void f(int x) {\n" " a = x+456;\n"