Skip to content

Commit b831428

Browse files
committed
testbufferoverrun; fixed TODO test cases for pointer arithmetic overflows
1 parent 680a1ee commit b831428

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

lib/checkbufferoverrun.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ void CheckBufferOverrun::pointerArithmetic()
436436
continue;
437437
if (!tok->valueType() || tok->valueType()->pointer == 0)
438438
continue;
439-
if (!tok->astOperand1() || !tok->astOperand2())
439+
if (!tok->isBinaryOp())
440440
continue;
441441
if (!tok->astOperand1()->valueType() || !tok->astOperand2()->valueType())
442442
continue;
@@ -472,7 +472,14 @@ void CheckBufferOverrun::pointerArithmetic()
472472
if (const ValueFlow::Value *neg = indexToken->getValueLE(-1, mSettings))
473473
pointerArithmeticError(tok, indexToken, neg);
474474
} else if (tok->str() == "-") {
475-
// TODO
475+
const Token *array = arrayToken;
476+
while (Token::Match(array, ".|::"))
477+
array = array->astOperand2();
478+
if (array->variable() && array->variable()->isArray()) {
479+
const ValueFlow::Value *v = indexToken->getValueGE(1, mSettings);
480+
if (v)
481+
pointerArithmeticError(tok, indexToken, v);
482+
}
476483
}
477484
}
478485
}

test/testbufferoverrun.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class TestBufferOverrun : public TestFixture {
198198
TEST_CASE(pointer_out_of_bounds_2);
199199
TEST_CASE(pointer_out_of_bounds_3);
200200
TEST_CASE(pointer_out_of_bounds_4);
201-
// TODO TEST_CASE(pointer_out_of_bounds_sub);
201+
TEST_CASE(pointer_out_of_bounds_sub);
202202

203203
TEST_CASE(strcat1);
204204

@@ -3039,14 +3039,14 @@ class TestBufferOverrun : public TestFixture {
30393039
" if (i == 123) {}\n"
30403040
" dostuff(x-i);\n"
30413041
"}");
3042-
ASSERT_EQUALS("[test.cpp:4]: (portability) Undefined behaviour, when 'i' is 123 the pointer arithmetic 'x-i' is out of bounds.\n", errout.str());
3042+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (portability) Undefined behaviour, when 'i' is 123 the pointer arithmetic 'x-i' is out of bounds.\n", errout.str());
30433043

30443044
check("void f(int i) {\n"
30453045
" char x[10];\n"
30463046
" if (i == -20) {}\n"
30473047
" dostuff(x-i);\n"
30483048
"}");
3049-
ASSERT_EQUALS("[test.cpp:4]: (portability) Undefined behaviour, when 'i' is -20 the pointer arithmetic 'x-i' is out of bounds.\n", errout.str());
3049+
TODO_ASSERT_EQUALS("[test.cpp:4]: (portability) Undefined behaviour, when 'i' is -20 the pointer arithmetic 'x-i' is out of bounds.\n", "", errout.str());
30503050
}
30513051

30523052
void strcat1() {

0 commit comments

Comments
 (0)