Skip to content

Commit d34de74

Browse files
authored
Fix 11306: FP knownConditionTrueFalse with strlen() (#4477)
* Fix 11306: FP knownConditionTrueFalse with strlen() * Add another test
1 parent 8126d5c commit d34de74

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5223,16 +5223,11 @@ static void valueFlowSymbolicOperators(TokenList* tokenlist, SymbolDatabase* sym
52235223
continue;
52245224
if (value.intvalue != 0)
52255225
continue;
5226-
if (value.bound == ValueFlow::Value::Bound::Upper)
5227-
continue;
5228-
if (value.isImpossible() && value.bound != ValueFlow::Value::Bound::Lower)
5229-
continue;
5230-
if (value.isKnown() && value.bound != ValueFlow::Value::Bound::Point)
5231-
continue;
52325226
const Token* strlenTok = isStrlenOf(value.tokvalue, arrayTok);
52335227
if (!strlenTok)
52345228
continue;
52355229
ValueFlow::Value v = value;
5230+
v.bound = ValueFlow::Value::Bound::Point;
52365231
v.valueType = ValueFlow::Value::ValueType::INT;
52375232
v.errorPath.emplace_back(strlenTok, "Return index of string to the first element that is 0");
52385233
setTokenValue(tok, v, tokenlist->getSettings());
@@ -8865,7 +8860,7 @@ void ValueFlow::setValues(TokenList *tokenlist, SymbolDatabase* symboldatabase,
88658860

88668861
std::size_t values = 0;
88678862
std::size_t n = 4;
8868-
while (n > 0 && values < getTotalValues(tokenlist)) {
8863+
while (n > 0 && values != getTotalValues(tokenlist)) {
88698864
values = getTotalValues(tokenlist);
88708865
valueFlowImpossibleValues(tokenlist, settings);
88718866
valueFlowSymbolicOperators(tokenlist, symboldatabase);

test/testvalueflow.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7585,6 +7585,23 @@ class TestValueFlow : public TestFixture {
75857585
" return 0;\n"
75867586
"}\n";
75877587
ASSERT_EQUALS(true, testValueOfXImpossible(code, 4U, 0));
7588+
7589+
code = "int f(char *s, size_t i) {\n"
7590+
" if (i < strlen(s)) {\n"
7591+
" int x = s[i] != ' ';\n"
7592+
" return x;\n"
7593+
" }\n"
7594+
" return 0;\n"
7595+
"}\n";
7596+
ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, 1));
7597+
7598+
code = "int f(char *s, size_t i) {\n"
7599+
" if (i == strlen(s)) {}\n"
7600+
" int x = s[i];\n"
7601+
" return x;\n"
7602+
"}\n";
7603+
ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, 0));
7604+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0));
75887605
}
75897606

75907607
void valueFlowSmartPointer()

0 commit comments

Comments
 (0)