Skip to content

Commit dbc8273

Browse files
committed
ValueFlow: improved abstract interpretation of for loops
1 parent c14a3d6 commit dbc8273

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -655,15 +655,15 @@ static void execute(const Token *expr,
655655
*error = true;
656656
}
657657

658-
else if (expr->str() == "++") {
658+
else if (expr->str() == "++" || expr->str() == "--") {
659659
if (!expr->astOperand1() || expr->astOperand1()->varId() == 0U)
660660
*error = true;
661661
else {
662662
std::map<unsigned int, MathLib::bigint>::iterator var = programMemory->find(expr->astOperand1()->varId());
663663
if (var == programMemory->end())
664664
*error = true;
665665
else {
666-
*result = var->second + 1;
666+
*result = var->second + (expr->str() == "++" ? 1 : -1);
667667
var->second = *result;
668668
}
669669
}
@@ -757,8 +757,7 @@ static bool valueFlowForLoop2(const Token *tok,
757757
if (error)
758758
return false;
759759
execute(secondExpression, &programMemory, &result, &error);
760-
if (error)
761-
return false;
760+
762761
std::map<unsigned int, MathLib::bigint> startMemory(programMemory);
763762
std::map<unsigned int, MathLib::bigint> endMemory;
764763

@@ -771,9 +770,10 @@ static bool valueFlowForLoop2(const Token *tok,
771770
}
772771

773772
memory1->swap(startMemory);
774-
memory2->swap(endMemory);
773+
if (!error)
774+
memory2->swap(endMemory);
775775

776-
return !error;
776+
return true;
777777
}
778778

779779
static void valueFlowForLoopSimplify(Token * const bodyStart, const unsigned int varid, const MathLib::bigint value, TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings)

‎test/testbufferoverrun.cpp‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,8 @@ class TestBufferOverrun : public TestFixture {
10471047
" for (int i = 3; 0 <= i; i--)\n"
10481048
" a[i] = i;\n"
10491049
"}");
1050-
ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: a\n", errout.str());
1050+
ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: a\n"
1051+
"[test.cpp:5]: (error) Array 'a[3]' accessed at index 3, which is out of bounds.\n", errout.str());
10511052

10521053
check("void f()\n"
10531054
"{\n"
@@ -1536,7 +1537,8 @@ class TestBufferOverrun : public TestFixture {
15361537
" buffer[i] = i;\n"
15371538
" }\n"
15381539
"}");
1539-
ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: buffer\n", errout.str());
1540+
ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: buffer\n"
1541+
"[test.cpp:5]: (error) Array 'buffer[9]' accessed at index 9, which is out of bounds.\n", errout.str());
15401542

15411543
// Correct access limits -> i from 9 to 0
15421544
check("void f() {\n"
@@ -1768,7 +1770,8 @@ class TestBufferOverrun : public TestFixture {
17681770
" data[i] = 0;\n"
17691771
" }\n"
17701772
"}");
1771-
ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: data\n", errout.str());
1773+
ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: data\n"
1774+
"[test.cpp:5]: (error) Array 'data[8]' accessed at index 10, which is out of bounds.\n", errout.str());
17721775

17731776
check("void f()\n"
17741777
"{\n"
@@ -1902,7 +1905,8 @@ class TestBufferOverrun : public TestFixture {
19021905
" a[i] = 0;\n"
19031906
" }\n"
19041907
"}");
1905-
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: a\n", errout.str());
1908+
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds: a\n"
1909+
"[test.cpp:4]: (error) Array 'a[10]' accessed at index 10, which is out of bounds.\n", errout.str());
19061910
}
19071911

19081912
void array_index_for_neq() {

‎test/testvalueflow.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ class TestValueFlow : public TestFixture {
645645
" for (int x = 0; x < 10; x = x / 0)\n"
646646
" a[x] = 0;\n"
647647
"}";
648-
testValueOfX(code, 3U, 0); // don't crash
648+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 0)); // don't crash
649649

650650
code = "void f() {\n"
651651
" for (int x = 0; x < 10; x++)\n"

0 commit comments

Comments
 (0)