From 72342de0daa77bba37d2fece41a3f2305da1608a Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Thu, 2 Jun 2022 16:24:11 -0500 Subject: [PATCH] Fix 11114: FP knownConditionTrueFalse across functions (#4163) * Fix 11114: FP knownConditionTrueFalse across functions * Format --- lib/valueflow.cpp | 8 ++++---- test/testvalueflow.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 4c520e1bf..7b185bcf5 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -219,17 +219,17 @@ static void setConditionalValues(const Token* tok, ValueFlow::Value& false_value) { if (Token::Match(tok, "==|!=|>=|<=")) { - true_value = ValueFlow::Value{tok, value}; + setConditionalValue(true_value, tok, value); const char* greaterThan = ">="; const char* lessThan = "<="; if (lhs) std::swap(greaterThan, lessThan); if (Token::simpleMatch(tok, greaterThan, strlen(greaterThan))) { - false_value = ValueFlow::Value{tok, value - 1}; + setConditionalValue(false_value, tok, value - 1); } else if (Token::simpleMatch(tok, lessThan, strlen(lessThan))) { - false_value = ValueFlow::Value{tok, value + 1}; + setConditionalValue(false_value, tok, value + 1); } else { - false_value = ValueFlow::Value{tok, value}; + setConditionalValue(false_value, tok, value); } } else { const char* greaterThan = ">"; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index c842111a4..40b5a5602 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -4382,6 +4382,20 @@ private: " }\n" "}\n"; ASSERT_EQUALS(false, testValueOfX(code, 8U, -1)); + + code = "typedef enum {\n" + " K0, K1\n" + "} K;\n" + "bool valid(Object *obj, K x) {\n" + " if (!obj || obj->kind != x)\n" + " return false;\n" + " return x == K0;\n" + "}\n" + "void f(Object *obj) {\n" + " if (valid(obj, K0)) {}\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfX(code, 7U, 0)); + ASSERT_EQUALS(false, testValueOfXKnown(code, 7U, 0)); } void valueFlowFunctionReturn() { const char *code;