Skip to content

Commit 84be7ed

Browse files
authored
Fix #10227: FP: pointer arithmetic out of bounds in unreachable expression (danmar#7057)
1 parent c2adbe3 commit 84be7ed

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/checkbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ void CheckBufferOverrun::pointerArithmetic()
500500
const std::vector<const Token *> indexTokens{indexToken};
501501
const std::vector<ValueFlow::Value>& indexValues =
502502
getOverrunIndexValues(tok, arrayToken, dimensions, indexTokens, path);
503-
if (!indexValues.empty())
503+
if (!indexValues.empty() && !isUnreachableOperand(tok))
504504
pointerArithmeticError(tok, indexToken, &indexValues.front());
505505
}
506506

test/testbufferoverrun.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class TestBufferOverrun : public TestFixture {
248248
TEST_CASE(pointer_out_of_bounds_2);
249249
TEST_CASE(pointer_out_of_bounds_3);
250250
TEST_CASE(pointer_out_of_bounds_4);
251+
TEST_CASE(pointer_out_of_bounds_5); // #10227
251252
TEST_CASE(pointer_out_of_bounds_sub);
252253

253254
TEST_CASE(strcat1);
@@ -3863,6 +3864,12 @@ class TestBufferOverrun : public TestFixture {
38633864
ASSERT_EQUALS("", errout_str());
38643865
}
38653866

3867+
void pointer_out_of_bounds_5() { // #10227
3868+
check("int foo(char str[6]) {\n"
3869+
" return !((0 && *(\"STRING\" + 14) == 0) || memcmp(str, \"STRING\", 6) == 0);\n"
3870+
"}\n");
3871+
ASSERT_EQUALS("", errout_str());
3872+
}
38663873

38673874
void pointer_out_of_bounds_sub() {
38683875
// extracttests.start: void dostuff(char *);

0 commit comments

Comments
 (0)