Value Flow: Improved abstract interpretation of arithmetical expressions

This commit is contained in:
Daniel Marjamäki 2014-03-22 19:02:33 +01:00
parent 77c871035b
commit b6276058da
3 changed files with 26 additions and 1 deletions

View File

@ -663,6 +663,22 @@ static void execute(const Token *expr,
} }
} }
else if (expr->isArithmeticalOp() && expr->astOperand1() && expr->astOperand2()) {
MathLib::bigint result1, result2;
execute(expr->astOperand1(), programMemory, &result1, error);
execute(expr->astOperand2(), programMemory, &result2, error);
if (expr->str() == "+")
*result = result1 + result2;
else if (expr->str() == "-")
*result = result1 - result2;
else if (expr->str() == "*")
*result = result1 * result2;
else if (expr->str() == "/")
*result = result1 / result2;
else if (expr->str() == "%")
*result = result1 % result2;
}
else if (expr->str() == "&&") { else if (expr->str() == "&&") {
execute(expr->astOperand1(), programMemory, result, error); execute(expr->astOperand1(), programMemory, result, error);
if (*error || *result == 0) if (*error || *result == 0)

View File

@ -830,7 +830,8 @@ private:
" for (int i = 0; i < 4; i+=2)\n" " for (int i = 0; i < 4; i+=2)\n"
" a[i] = 0;\n" " a[i] = 0;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: a\n"
"[test.cpp:4]: (error) Array 'a[2]' accessed at index 2, which is out of bounds.\n", errout.str());
check("void f() {\n" // #4398 check("void f() {\n" // #4398
" int a[2];\n" " int a[2];\n"

View File

@ -626,6 +626,14 @@ private:
ASSERT_EQUALS(true, testValueOfX(code, 3U, 9)); ASSERT_EQUALS(true, testValueOfX(code, 3U, 9));
ASSERT_EQUALS(false, testValueOfX(code, 3U, 10)); ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
code = "void f() {\n"
" for (int x = 0; x < 10; x = x + 2)\n"
" a[x] = 0;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0));
ASSERT_EQUALS(true, testValueOfX(code, 3U, 8));
ASSERT_EQUALS(false, testValueOfX(code, 3U, 10));
code = "void f() {\n" code = "void f() {\n"
" for (int x = 0; x < 10; x++)\n" " for (int x = 0; x < 10; x++)\n"
" x<4 ?\n" " x<4 ?\n"