Skip to content

Commit 5218ded

Browse files
authored
Propagate zero multiplication on possible values as well (danmar#7434)
1 parent 1195160 commit 5218ded

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

lib/vf_settokenvalue.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -457,21 +457,11 @@ namespace ValueFlow
457457
if (noninvertible && value.isImpossible())
458458
return;
459459

460-
// known result when a operand is 0.
461-
if (Token::Match(parent, "[&*]") && astIsIntegral(parent, true) && value.isKnown() && value.isIntValue() &&
462-
value.intvalue == 0) {
463-
setTokenValue(parent, std::move(value), settings);
464-
return;
465-
}
466-
467-
// known result when a operand is true.
468-
if (Token::simpleMatch(parent, "&&") && value.isKnown() && value.isIntValue() && value.intvalue==0) {
469-
setTokenValue(parent, std::move(value), settings);
470-
return;
471-
}
472-
473-
// known result when a operand is false.
474-
if (Token::simpleMatch(parent, "||") && value.isKnown() && value.isIntValue() && value.intvalue!=0) {
460+
if (!value.isImpossible() && value.isIntValue() &&
461+
((Token::Match(parent, "[&*]") && astIsIntegral(parent, true) && value.intvalue == 0) ||
462+
(Token::simpleMatch(parent, "&&") && value.intvalue == 0) ||
463+
(Token::simpleMatch(parent, "||") && value.intvalue != 0))) {
464+
value.bound = Value::Bound::Point;
475465
setTokenValue(parent, std::move(value), settings);
476466
return;
477467
}

test/testvalueflow.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,13 @@ class TestValueFlow : public TestFixture {
36803680
"}\n";
36813681
ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 0));
36823682
ASSERT_EQUALS(true, testValueOfX(code, 9U, 0));
3683+
3684+
code = "int f(int a, int b) {\n"
3685+
" if (a > 0 && b > 0) {}\n"
3686+
" int x = a * b;\n"
3687+
" return x;\n"
3688+
"}\n";
3689+
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0));
36833690
}
36843691

36853692
void valueFlowAfterConditionTernary()

0 commit comments

Comments
 (0)