Skip to content

Commit b627605

Browse files
committed
Value Flow: Improved abstract interpretation of arithmetical expressions
1 parent 77c8710 commit b627605

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎lib/valueflow.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,22 @@ static void execute(const Token *expr,
663663
}
664664
}
665665

666+
else if (expr->isArithmeticalOp() && expr->astOperand1() && expr->astOperand2()) {
667+
MathLib::bigint result1, result2;
668+
execute(expr->astOperand1(), programMemory, &result1, error);
669+
execute(expr->astOperand2(), programMemory, &result2, error);
670+
if (expr->str() == "+")
671+
*result = result1 + result2;
672+
else if (expr->str() == "-")
673+
*result = result1 - result2;
674+
else if (expr->str() == "*")
675+
*result = result1 * result2;
676+
else if (expr->str() == "/")
677+
*result = result1 / result2;
678+
else if (expr->str() == "%")
679+
*result = result1 % result2;
680+
}
681+
666682
else if (expr->str() == "&&") {
667683
execute(expr->astOperand1(), programMemory, result, error);
668684
if (*error || *result == 0)

‎test/testbufferoverrun.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,8 @@ class TestBufferOverrun : public TestFixture {
830830
" for (int i = 0; i < 4; i+=2)\n"
831831
" a[i] = 0;\n"
832832
"}");
833-
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: a\n", errout.str());
833+
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: a\n"
834+
"[test.cpp:4]: (error) Array 'a[2]' accessed at index 2, which is out of bounds.\n", errout.str());
834835

835836
check("void f() {\n" // #4398
836837
" int a[2];\n"

‎test/testvalueflow.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,14 @@ class TestValueFlow : public TestFixture {
626626
ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
627627
ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
628628

629+
code = "void f() {\n"
630+
" for (int x = 0; x < 10; x = x + 2)\n"
631+
" a[x] = 0;\n"
632+
"}";
633+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
634+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 8));
635+
ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
636+
629637
code = "void f() {\n"
630638
" for (int x = 0; x < 10; x++)\n"
631639
" x<4 ?\n"

0 commit comments

Comments
 (0)