From dd69d5eabe7f5db4f3f0c3d195e06e2960c09b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 6 Nov 2016 17:42:01 +0100 Subject: [PATCH] ValueFlow: Don't calculate result when there is UB --- lib/valueflow.cpp | 4 ++-- test/testvalueflow.cpp | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) 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"