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> elseValues;
bool inverted = cond.inverted;
Token* ctx = skipNotAndCasts(condTok, &inverted);
bool then = cond.inverted ? !inverted : true;
if (!Token::Match(condTok, "!=|=|(|.") && condTok != cond.vartok) {
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);
}
if (!Token::Match(condTok, "==|!")) {
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);
if (cond.isBool())
insertNegateKnown(thenValues, cond.true_values);
}
}
bool inverted = cond.inverted;
Token* ctx = skipNotAndCasts(condTok, &inverted);
if (inverted)
std::swap(thenValues, elseValues);

View File

@ -6055,7 +6055,7 @@ private:
" return 0;\n"
" return v[0];\n"
"}\n";
ASSERT_EQUALS(0U, tokenValues(code, "v [ 0 ]").size());
ASSERT_EQUALS("", isImpossibleContainerSizeValue(tokenValues(code, "v [ 0 ]"), 0));
// container size => yields
code = "void f() {\n"
@ -6360,6 +6360,13 @@ private:
" return x;\n"
"}\n";
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()