Skip to content

Commit efe48e6

Browse files
committed
argument bounds validation for bitmap.blit()
1 parent beb4a79 commit efe48e6

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
212212

213213
displayio_bitmap_t *self = MP_OBJ_TO_PTR(pos_args[0]);
214214

215-
int16_t x = args[ARG_x].u_int;
216-
int16_t y = args[ARG_y].u_int;
215+
// Check x,y are within self (target) bitmap boundary
216+
int16_t x = mp_arg_validate_int_range(args[ARG_x].u_int, 0, self->width - 1, MP_QSTR_x);
217+
int16_t y = mp_arg_validate_int_range(args[ARG_y].u_int, 0, self->height - 1, MP_QSTR_y);
217218

218219
displayio_bitmap_t *source = mp_arg_validate_type(args[ARG_source].u_obj, &displayio_bitmap_type, MP_QSTR_source_bitmap);
219220

@@ -223,32 +224,21 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
223224
mp_raise_ValueError(translate("source palette too large"));
224225
}
225226

226-
int16_t x1 = args[ARG_x1].u_int;
227-
int16_t y1 = args[ARG_y1].u_int;
227+
// Check x1,y1,x2,y2 are within source bitmap boundary
228+
int16_t x1 = mp_arg_validate_int_range(args[ARG_x1].u_int, 0, source->width - 1, MP_QSTR_x1);
229+
int16_t y1 = mp_arg_validate_int_range(args[ARG_y1].u_int, 0, source->height - 1, MP_QSTR_y1);
228230
int16_t x2, y2;
229231
// if x2 or y2 is None, then set as the maximum size of the source bitmap
230232
if (args[ARG_x2].u_obj == mp_const_none) {
231233
x2 = source->width;
232234
} else {
233-
x2 = mp_obj_get_int(args[ARG_x2].u_obj);
235+
x2 = mp_arg_validate_int_range(mp_obj_get_int(args[ARG_x2].u_obj), 0, source->width, MP_QSTR_x2);
234236
}
235237
// int16_t y2;
236238
if (args[ARG_y2].u_obj == mp_const_none) {
237239
y2 = source->height;
238240
} else {
239-
y2 = mp_obj_get_int(args[ARG_y2].u_obj);
240-
}
241-
242-
// Check x,y are within self (target) bitmap boundary
243-
if ((x < 0) || (y < 0) || (x > self->width) || (y > self->height)) {
244-
mp_raise_ValueError(translate("out of range of target"));
245-
}
246-
// Check x1,y1,x2,y2 are within source bitmap boundary
247-
if ((x1 < 0) || (x1 > source->width) ||
248-
(y1 < 0) || (y1 > source->height) ||
249-
(x2 < 0) || (x2 > source->width) ||
250-
(y2 < 0) || (y2 > source->height)) {
251-
mp_raise_ValueError(translate("out of range of source"));
241+
y2 = mp_arg_validate_int_range(mp_obj_get_int(args[ARG_y2].u_obj), 0, source->height, MP_QSTR_y2);
252242
}
253243

254244
// Ensure x1 < x2 and y1 < y2

0 commit comments

Comments
 (0)