ValueFlow: Don't calculate result when there is UB

This commit is contained in:
Daniel Marjamäki 2016-11-06 17:42:01 +01:00
parent 483fd8682a
commit dd69d5eabe
2 changed files with 8 additions and 2 deletions

View File

@ -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;

View File

@ -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"