Fix 11114: FP knownConditionTrueFalse across functions (#4163)

* Fix 11114: FP knownConditionTrueFalse across functions

* Format
This commit is contained in:
Paul Fultz II 2022-06-02 16:24:11 -05:00 committed by GitHub
parent 133b9c2790
commit 72342de0da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -219,17 +219,17 @@ static void setConditionalValues(const Token* tok,
ValueFlow::Value& false_value) ValueFlow::Value& false_value)
{ {
if (Token::Match(tok, "==|!=|>=|<=")) { if (Token::Match(tok, "==|!=|>=|<=")) {
true_value = ValueFlow::Value{tok, value}; setConditionalValue(true_value, tok, value);
const char* greaterThan = ">="; const char* greaterThan = ">=";
const char* lessThan = "<="; const char* lessThan = "<=";
if (lhs) if (lhs)
std::swap(greaterThan, lessThan); std::swap(greaterThan, lessThan);
if (Token::simpleMatch(tok, greaterThan, strlen(greaterThan))) { 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))) { } else if (Token::simpleMatch(tok, lessThan, strlen(lessThan))) {
false_value = ValueFlow::Value{tok, value + 1}; setConditionalValue(false_value, tok, value + 1);
} else { } else {
false_value = ValueFlow::Value{tok, value}; setConditionalValue(false_value, tok, value);
} }
} else { } else {
const char* greaterThan = ">"; const char* greaterThan = ">";

View File

@ -4382,6 +4382,20 @@ private:
" }\n" " }\n"
"}\n"; "}\n";
ASSERT_EQUALS(false, testValueOfX(code, 8U, -1)); 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() { void valueFlowFunctionReturn() {
const char *code; const char *code;