Skip to content

Commit 530e59f

Browse files
Fix #15184 FN zerodiv with compound assignment (#9006)
1 parent 9ee9bd1 commit 530e59f

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎lib/calculate.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,43 +60,53 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr)
6060
constexpr MathLib::bigint maxBitsSignedShift = maxBitsShift - 1;
6161
switch (MathLib::encodeMultiChar(s)) {
6262
case '+':
63+
case '+=':
6364
return wrap(x + y);
6465
case '-':
66+
case '-=':
6567
return wrap(x - y);
6668
case '*':
69+
case '*=':
6770
return wrap(x * y);
6871
case '/':
72+
case '/=':
6973
if (isZero(y) || (std::is_signed<T>{} && y < 0)) {
7074
if (error)
7175
*error = true;
7276
return R{};
7377
}
7478
return wrap(x / y);
7579
case '%':
80+
case '%=':
7681
if (isZero(MathLib::bigint(y)) || (std::is_signed<T>{} && MathLib::bigint(y) < 0)) {
7782
if (error)
7883
*error = true;
7984
return R{};
8085
}
8186
return wrap(MathLib::bigint(x) % MathLib::bigint(y));
8287
case '&':
88+
case '&=':
8389
return wrap(MathLib::bigint(x) & MathLib::bigint(y));
8490
case '|':
91+
case '|=':
8592
return wrap(MathLib::bigint(x) | MathLib::bigint(y));
8693
case '^':
94+
case '^=':
8795
return wrap(MathLib::bigint(x) ^ MathLib::bigint(y));
8896
case '>':
8997
return wrap(x > y);
9098
case '<':
9199
return wrap(x < y);
92100
case '<<':
101+
case '<<=':
93102
if (y >= maxBitsSignedShift || y < 0 || x < 0) {
94103
if (error)
95104
*error = true;
96105
return R{};
97106
}
98107
return wrap(MathLib::bigint(x) << MathLib::bigint(y));
99108
case '>>':
109+
case '>>=':
100110
if (y >= maxBitsSignedShift || y < 0 || x < 0) {
101111
if (error)
102112
*error = true;

‎lib/vf_settokenvalue.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ namespace ValueFlow
486486
}
487487

488488
// Calculations..
489-
else if ((parent->isArithmeticalOp() || parent->isComparisonOp() || (parent->tokType() == Token::eBitOp) ||
489+
else if ((parent->isArithmeticalOp() || parent->isComparisonOp() || (parent->tokType() == Token::eBitOp) || (Token::Match(parent, "%assign%") && parent->str().size() > 1) ||
490490
(parent->tokType() == Token::eLogicalOp)) &&
491491
parent->astOperand1() && parent->astOperand2()) {
492492

‎test/testvalueflow.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,12 @@ class TestValueFlow : public TestFixture {
14051405
"}\n";
14061406
ASSERT_NO_THROW(tokenValues(code, "<=>"));
14071407

1408+
code = "int f() {\n" // #15184
1409+
" int i = -1;\n"
1410+
" return 1 / (i += 1);\n"
1411+
"}\n";
1412+
ASSERT_EQUALS(true, testKnownValueOfTok(code, "+=", 0));
1413+
14081414
// Comparison of string
14091415
values = removeImpossible(tokenValues("f(\"xyz\" == \"xyz\");\n", "==")); // implementation defined
14101416
ASSERT_EQUALS(0U, values.size()); // <- no value

0 commit comments

Comments
 (0)