From ef35ca1d3ebe4f1835e32e52fbd81225485ae057 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 23 Jun 2022 08:36:22 -0500 Subject: [PATCH] vectorio: Simplify argument checking of x/y values --- locale/circuitpython.pot | 1 - shared-bindings/displayio/Shape.c | 6 +++--- shared-module/vectorio/Polygon.c | 26 ++++++++------------------ shared-module/vectorio/VectorShape.c | 8 ++------ 4 files changed, 13 insertions(+), 28 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 52f72e8bee..9e50e4c94b 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -4181,7 +4181,6 @@ msgid "unreadable attribute" msgstr "" #: shared-bindings/displayio/TileGrid.c shared-bindings/vectorio/VectorShape.c -#: shared-module/vectorio/Polygon.c shared-module/vectorio/VectorShape.c msgid "unsupported %q type" msgstr "" diff --git a/shared-bindings/displayio/Shape.c b/shared-bindings/displayio/Shape.c index d5e907ac38..4bec17608c 100644 --- a/shared-bindings/displayio/Shape.c +++ b/shared-bindings/displayio/Shape.c @@ -80,9 +80,9 @@ STATIC mp_obj_t displayio_shape_make_new(const mp_obj_type_t *type, size_t n_arg STATIC mp_obj_t displayio_shape_obj_set_boundary(size_t n_args, const mp_obj_t *args) { (void)n_args; displayio_shape_t *self = MP_OBJ_TO_PTR(args[0]); - mp_int_t y = mp_arg_validate_type_int(args[1], MP_ARG_y); - mp_int_t start_x = mp_arg_validate_type_int(args[1], MP_ARG_start_x); - mp_int_t end_x = mp_arg_validate_type_int(args[1], MP_ARG_end_x); + mp_int_t y = mp_arg_validate_type_int(args[1], MP_QSTR_y); + mp_int_t start_x = mp_arg_validate_type_int(args[1], MP_QSTR_start_x); + mp_int_t end_x = mp_arg_validate_type_int(args[1], MP_QSTR_end_x); common_hal_displayio_shape_set_boundary(self, y, start_x, end_x); return mp_const_none; diff --git a/shared-module/vectorio/Polygon.c b/shared-module/vectorio/Polygon.c index 24eb1c501d..e24893af01 100644 --- a/shared-module/vectorio/Polygon.c +++ b/shared-module/vectorio/Polygon.c @@ -14,7 +14,8 @@ // Converts a list of points tuples to a flat list of ints for speedier internal use. -// Also validates the points. +// Also validates the points. If this fails due to invalid types or values, the +// content of the points is undefined. static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple_list) { size_t len = 0; mp_obj_t *items; @@ -26,12 +27,8 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple } if (self->len < 2 * len) { - if (self->points_list != NULL) { - VECTORIO_POLYGON_DEBUG("free(%d), ", sizeof(self->points_list)); - gc_free(self->points_list); - } - self->points_list = gc_alloc(2 * len * sizeof(uint16_t), false, false); - VECTORIO_POLYGON_DEBUG("alloc(%p, %d)", self->points_list, 2 * len * sizeof(uint16_t)); + self->points_list = gc_realloc(self->points_list, 2 * len * sizeof(uint16_t), true); + VECTORIO_POLYGON_DEBUG("realloc(%d) -> %p", self->points_list, 2 * len * sizeof(uint16_t)); } self->len = 2 * len; @@ -42,17 +39,10 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple mp_arg_validate_length(tuple_len, 2, MP_QSTR_point); - mp_int_t x; - mp_int_t y; - if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x) - || !mp_obj_get_int_maybe(tuple_items[ 1 ], &y) - || x < SHRT_MIN || x > SHRT_MAX || y < SHRT_MIN || y > SHRT_MAX - ) { - gc_free(self->points_list); - self->points_list = NULL; - mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); - self->len = 0; - } + mp_int_t x = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_x); + mp_arg_validate_int_range(x, SHRT_MIN, SHRT_MAX, MP_QSTR_x); + mp_int_t y = mp_arg_validate_type_int(tuple_items[1], MP_QSTR_y); + mp_arg_validate_int_range(y, SHRT_MIN, SHRT_MAX, MP_QSTR_y); self->points_list[2 * i ] = (int16_t)x; self->points_list[2 * i + 1] = (int16_t)y; } diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/VectorShape.c index f72cd5cc97..d9c13f54cc 100644 --- a/shared-module/vectorio/VectorShape.c +++ b/shared-module/vectorio/VectorShape.c @@ -277,12 +277,8 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self mp_obj_tuple_get(xy, &tuple_len, &tuple_items); mp_arg_validate_length(tuple_len, 2, MP_QSTR_location); - mp_int_t x; - mp_int_t y; - if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x) - || !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) { - mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point); - } + mp_int_t x = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_x); + mp_int_t y = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_y); bool dirty = false; if (self->x != x) { check_bounds_and_set_x(self, x);