Skip to content

Commit a43cfdd

Browse files
dpgeorgejepler
authored andcommitted
py/vm: Prevent array bound warning when using -MP_OBJ_ITER_BUF_NSLOTS.
This warning can happen on clang 13.0.1 building mpy-cross: ../py/vm.c:748:25: error: array index -3 refers past the last possible element for an array in 64-bit address space containing 64-bit (8-byte) elements (max possible 2305843009213693952 elements) [-Werror,-Warray-bounds] sp[-MP_OBJ_ITER_BUF_NSLOTS + 1] = MP_OBJ_NULL; ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Using pointer access instead of array access works around this warning. Fixes issue #8467. Signed-off-by: Damien George <[email protected]>
1 parent faca1ec commit a43cfdd

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

py/vm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,8 @@ unwind_jump:;
821821
obj = mp_getiter(obj, iter_buf);
822822
if (obj != MP_OBJ_FROM_PTR(iter_buf)) {
823823
// Iterator didn't use the stack so indicate that with MP_OBJ_NULL.
824-
sp[-MP_OBJ_ITER_BUF_NSLOTS + 1] = MP_OBJ_NULL;
825-
sp[-MP_OBJ_ITER_BUF_NSLOTS + 2] = obj;
824+
*(sp - MP_OBJ_ITER_BUF_NSLOTS + 1) = MP_OBJ_NULL;
825+
*(sp - MP_OBJ_ITER_BUF_NSLOTS + 2) = obj;
826826
}
827827
DISPATCH();
828828
}
@@ -833,8 +833,8 @@ unwind_jump:;
833833
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
834834
code_state->sp = sp;
835835
mp_obj_t obj;
836-
if (sp[-MP_OBJ_ITER_BUF_NSLOTS + 1] == MP_OBJ_NULL) {
837-
obj = sp[-MP_OBJ_ITER_BUF_NSLOTS + 2];
836+
if (*(sp - MP_OBJ_ITER_BUF_NSLOTS + 1) == MP_OBJ_NULL) {
837+
obj = *(sp - MP_OBJ_ITER_BUF_NSLOTS + 2);
838838
} else {
839839
obj = MP_OBJ_FROM_PTR(&sp[-MP_OBJ_ITER_BUF_NSLOTS + 1]);
840840
}

0 commit comments

Comments
 (0)