Skip to content

Commit bb4cccc

Browse files
committed
use range instead of min where applicable to consolodate bounds checks
1 parent efe48e6 commit bb4cccc

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

shared-bindings/displayio/Bitmap.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,15 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
148148
} else {
149149
mp_obj_t *items;
150150
mp_obj_get_array_fixed_n(index_obj, 2, &items);
151-
x = mp_arg_validate_int_min(mp_obj_get_int(items[0]), 0, MP_QSTR_x);
152-
y = mp_arg_validate_int_min(mp_obj_get_int(items[1]), 0, MP_QSTR_y);
153-
if (x >= common_hal_displayio_bitmap_get_width(self) || y >= common_hal_displayio_bitmap_get_height(self)) {
154-
mp_raise_IndexError(translate("pixel coordinates out of bounds"));
155-
}
151+
x = mp_arg_validate_int_range(mp_obj_get_int(items[0]), 0, self->width - 1, MP_QSTR_x);
152+
y = mp_arg_validate_int_range(mp_obj_get_int(items[1]), 0, self->height - 1, MP_QSTR_y);
156153
}
157154

158155
if (value_obj == MP_OBJ_SENTINEL) {
159156
// load
160157
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_bitmap_get_pixel(self, x, y));
161158
} else {
162-
mp_uint_t value = (mp_uint_t)mp_obj_get_int(value_obj);
163-
if ((value >> common_hal_displayio_bitmap_get_bits_per_value(self)) != 0) {
164-
mp_raise_ValueError(translate("pixel value requires too many bits"));
165-
}
159+
mp_uint_t value = (mp_uint_t)mp_arg_validate_int_range(mp_obj_get_int(value_obj), 0,(1u << common_hal_displayio_bitmap_get_bits_per_value(self)) - 1, MP_QSTR_value);
166160
common_hal_displayio_bitmap_set_pixel(self, x, y, value);
167161
}
168162
return mp_const_none;
@@ -276,10 +270,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 1, displayio_bitmap_obj_bl
276270
STATIC mp_obj_t displayio_bitmap_obj_fill(mp_obj_t self_in, mp_obj_t value_obj) {
277271
displayio_bitmap_t *self = MP_OBJ_TO_PTR(self_in);
278272

279-
mp_uint_t value = (mp_uint_t)mp_arg_validate_int_min(mp_obj_get_int(value_obj), 0, MP_QSTR_value);
280-
if ((value >> common_hal_displayio_bitmap_get_bits_per_value(self)) != 0) {
281-
mp_raise_ValueError(translate("pixel value requires too many bits"));
282-
}
273+
mp_uint_t value = (mp_uint_t)mp_arg_validate_int_range(mp_obj_get_int(value_obj), 0,(1u << common_hal_displayio_bitmap_get_bits_per_value(self)) - 1,MP_QSTR_value);
283274
common_hal_displayio_bitmap_fill(self, value);
284275

285276
return mp_const_none;

0 commit comments

Comments
 (0)