Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 1 addition & 6 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,7 @@ static void valueFlowNumber(TokenList &tokenlist, const Settings& settings)

if (tokenlist.isCPP() || settings.standards.c >= Standards::C23) {
for (Token *tok = tokenlist.front(); tok; tok = tok->next()) {
if (tok->isName() && !tok->varId() && Token::Match(tok, "%bool%")) {
ValueFlow::Value value(tok->str() == "true");
if (!tok->isTemplateArg())
value.setKnown();
setTokenValue(tok, std::move(value), settings);
} else if (Token::Match(tok, "[(,] NULL [,)]")) {
if (Token::Match(tok, "[(,] NULL [,)]")) {
// NULL function parameters are not simplified in the
// normal tokenlist
ValueFlow::Value value(0);
Expand Down
5 changes: 5 additions & 0 deletions lib/vf_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@
if (!tok->isTemplateArg())
value.setKnown();
setTokenValue(tok, std::move(value), settings);
} else if ((tok->isCpp() || settings.standards.c >= Standards::C23) && (tok->isName() && !tok->varId() && Token::Match(tok, "%bool%"))) {

Check warning

Code scanning / Cppcheck Premium

Perform explicit tests to determine success, true and false, and equality. Warning

Perform explicit tests to determine success, true and false, and equality.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Value value(tok->str() == "true");
if (!tok->isTemplateArg())
value.setKnown();
setTokenValue(tok, std::move(value), settings);
} else if (Token::simpleMatch(tok, "sizeof (")) {
if (tok->next()->astOperand2() && !tok->next()->astOperand2()->isLiteral() && tok->next()->astOperand2()->valueType() &&
(tok->next()->astOperand2()->valueType()->pointer == 0 || // <- TODO this is a bailout, abort when there are array->pointer conversions
Expand Down
10 changes: 10 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,16 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(123, values.empty() ? 0 : values.front().intvalue);

code = "x = true ? 2 : 3;\n"; // #14369
values = tokenValues(code, "?");
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(2, values.empty() ? 0 : values.front().intvalue);

code = "x = false ? 2 : 3;\n"; // #14369
values = tokenValues(code, "?");
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(3, values.empty() ? 0 : values.front().intvalue);

code = "int f() {\n"
" const int i = 1;\n"
" int x = i < 0 ? 0 : 1;\n"
Expand Down
Loading