Fix 11548: FP knownConditionTrueFalse with empty() == false (#4788)

* Fix 11548: FP knownConditionTrueFalse with empty() == false

* Format

* Fix test
This commit is contained in:
Paul Fultz II 2023-02-11 03:42:13 -06:00 committed by GitHub
parent a846bc2d99
commit 2b9af94b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -6157,22 +6157,24 @@ struct ConditionHandler {
std::list<ValueFlow::Value> thenValues; std::list<ValueFlow::Value> thenValues;
std::list<ValueFlow::Value> elseValues; std::list<ValueFlow::Value> elseValues;
bool inverted = cond.inverted;
Token* ctx = skipNotAndCasts(condTok, &inverted);
bool then = cond.inverted ? !inverted : true;
if (!Token::Match(condTok, "!=|=|(|.") && condTok != cond.vartok) { if (!Token::Match(condTok, "!=|=|(|.") && condTok != cond.vartok) {
thenValues.insert(thenValues.end(), cond.true_values.cbegin(), cond.true_values.cend()); thenValues.insert(thenValues.end(), cond.true_values.cbegin(), cond.true_values.cend());
if (allowImpossible && isConditionKnown(condTok, false)) if (allowImpossible && isConditionKnown(ctx, !then))
insertImpossible(elseValues, cond.false_values); insertImpossible(elseValues, cond.false_values);
} }
if (!Token::Match(condTok, "==|!")) { if (!Token::Match(condTok, "==|!")) {
elseValues.insert(elseValues.end(), cond.false_values.cbegin(), cond.false_values.cend()); elseValues.insert(elseValues.end(), cond.false_values.cbegin(), cond.false_values.cend());
if (allowImpossible && isConditionKnown(condTok, true)) { if (allowImpossible && isConditionKnown(ctx, then)) {
insertImpossible(thenValues, cond.true_values); insertImpossible(thenValues, cond.true_values);
if (cond.isBool()) if (cond.isBool())
insertNegateKnown(thenValues, cond.true_values); insertNegateKnown(thenValues, cond.true_values);
} }
} }
bool inverted = cond.inverted;
Token* ctx = skipNotAndCasts(condTok, &inverted);
if (inverted) if (inverted)
std::swap(thenValues, elseValues); std::swap(thenValues, elseValues);

View File

@ -6055,7 +6055,7 @@ private:
" return 0;\n" " return 0;\n"
" return v[0];\n" " return v[0];\n"
"}\n"; "}\n";
ASSERT_EQUALS(0U, tokenValues(code, "v [ 0 ]").size()); ASSERT_EQUALS("", isImpossibleContainerSizeValue(tokenValues(code, "v [ 0 ]"), 0));
// container size => yields // container size => yields
code = "void f() {\n" code = "void f() {\n"
@ -6360,6 +6360,13 @@ private:
" return x;\n" " return x;\n"
"}\n"; "}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 1)); ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 1));
// #11548
code = "void f(const std::string& a, const std::string& b) {\n"
" if (a.empty() && b.empty()) {}\n"
" else if (a.empty() == false && b.empty() == false) {}\n"
"}\n";
ASSERT("" != isImpossibleContainerSizeValue(tokenValues(code, "a . empty ( ) == false"), 0));
} }
void valueFlowContainerElement() void valueFlowContainerElement()