Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5164,8 +5164,9 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin
setTokenValue(tok, std::move(value), settings);
}
}
} else if (Token::Match(tok->astParent(), "?|&&|!|%oror%") ||
} else if (Token::Match(tok->astParent(), "&&|!|%oror%") ||
Token::Match(tok->astParent()->previous(), "if|while (") ||
(astIsLHS(tok) && Token::simpleMatch(tok->astParent(), "?")) ||
((astIsPointer(tok) || tok->function()) && isUsedAsBool(tok, settings))) {
std::vector<ValueFlow::Value> result = infer(makeIntegralInferModel(), "!=", tok->values(), 0);
if (result.size() != 1)
Expand Down
3 changes: 1 addition & 2 deletions lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ namespace ValueFlow
visitAstNodes(parent->astOperand1(),
[&](const Token *t) {
if (t->varId()) {
if (varId > 0 || value.varId != 0)
if (varId > 0)
ret = true;
varId = t->varId();
} else if (t->str() == "(" && Token::Match(t->previous(), "%name%"))
Expand All @@ -443,7 +443,6 @@ namespace ValueFlow
});
if (ret)
return;

value.conditional = true;
value.changeKnownToPossible();

Expand Down
39 changes: 39 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4325,6 +4325,45 @@ class TestValueFlow : public TestFixture {
"}\n";
auto values = tokenValues(code, "s :", ValueFlow::Value::ValueType::FLOAT);
ASSERT_EQUALS(0, values.size());

code = "int a[5];\n" // 15034
"int g(int i) {\n"
" return a[i < 0 ? -i : i];\n"
"}\n"
"int f() {\n"
" return g(-5);\n"
"}\n";
values = tokenValues(code, "?");
ASSERT_EQUALS(2, values.size());
auto it = values.begin();
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueType::INT, it->valueType);
ASSERT_EQUALS(0, it->intvalue);
ASSERT_EQUALS_ENUM(ValueFlow::Value::Bound::Lower, it->bound);
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Possible, it->valueKind);
++it;
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueType::INT, it->valueType);
ASSERT_EQUALS(5, it->intvalue);
ASSERT_EQUALS_ENUM(ValueFlow::Value::Bound::Point, it->bound);
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Possible, it->valueKind);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ticket is about an arrayIndexOutOfBounds false negative, but there's no checker-level test for it. Please add the ticket example to testbufferoverrun.cpp, together with a negative case such as a[i < 0 ? 0 : i] called with g(-5), which must not warn.


code = "int f(int a, int b) {\n"
" if (b <= 0)\n"
" return b;\n"
" int a = (a > 0) ? a : -a;\n"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This snippet isn't valid C/C++: int a = (a > 0) ? a : -a; redeclares the parameter a in the function's outermost block, and it initialises a from itself. Running cppcheck on it gives legacyUninitvar on line 4. Please use a fresh variable, e.g. int c = (a > 0) ? a : -a; return 1 / (c < b ? b : c);, which gives the same value-flow shape.

Also, the two values below are asserted with identical fields (SYMBOLIC, -1, Upper, Impossible), and it->tokvalue is never checked. That means the test can't tell the !<=symbolic=(a-1) value from the !<=symbolic=(b-1) value. Checking the symbol, e.g. ASSERT_EQUALS("b", it->tokvalue->expressionString()), or using isImpossibleContainerSizeValue-style helpers would make the test pin down the intended behaviour.

" return 1 / (a < b ? b : a);\n"
"}\n";
values = tokenValues(code, "? b");
ASSERT_EQUALS(2, values.size());
it = values.begin();
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueType::SYMBOLIC, it->valueType);
ASSERT_EQUALS(-1, it->intvalue);
ASSERT_EQUALS_ENUM(ValueFlow::Value::Bound::Upper, it->bound);
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Impossible, it->valueKind);
++it;
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueType::SYMBOLIC, it->valueType);
ASSERT_EQUALS(-1, it->intvalue);
ASSERT_EQUALS_ENUM(ValueFlow::Value::Bound::Upper, it->bound);
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Impossible, it->valueKind);
}

void valueFlowForwardLambda() {
Expand Down
Loading