Skip to content

Commit ef35ca1

Browse files
committed
vectorio: Simplify argument checking of x/y values
1 parent 267ec1d commit ef35ca1

File tree

4 files changed

+13
-28
lines changed

4 files changed

+13
-28
lines changed

locale/circuitpython.pot

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4181,7 +4181,6 @@ msgid "unreadable attribute"
41814181
msgstr ""
41824182

41834183
#: shared-bindings/displayio/TileGrid.c shared-bindings/vectorio/VectorShape.c
4184-
#: shared-module/vectorio/Polygon.c shared-module/vectorio/VectorShape.c
41854184
msgid "unsupported %q type"
41864185
msgstr ""
41874186

shared-bindings/displayio/Shape.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ STATIC mp_obj_t displayio_shape_make_new(const mp_obj_type_t *type, size_t n_arg
8080
STATIC mp_obj_t displayio_shape_obj_set_boundary(size_t n_args, const mp_obj_t *args) {
8181
(void)n_args;
8282
displayio_shape_t *self = MP_OBJ_TO_PTR(args[0]);
83-
mp_int_t y = mp_arg_validate_type_int(args[1], MP_ARG_y);
84-
mp_int_t start_x = mp_arg_validate_type_int(args[1], MP_ARG_start_x);
85-
mp_int_t end_x = mp_arg_validate_type_int(args[1], MP_ARG_end_x);
83+
mp_int_t y = mp_arg_validate_type_int(args[1], MP_QSTR_y);
84+
mp_int_t start_x = mp_arg_validate_type_int(args[1], MP_QSTR_start_x);
85+
mp_int_t end_x = mp_arg_validate_type_int(args[1], MP_QSTR_end_x);
8686
common_hal_displayio_shape_set_boundary(self, y, start_x, end_x);
8787

8888
return mp_const_none;

shared-module/vectorio/Polygon.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515

1616
// Converts a list of points tuples to a flat list of ints for speedier internal use.
17-
// Also validates the points.
17+
// Also validates the points. If this fails due to invalid types or values, the
18+
// content of the points is undefined.
1819
static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple_list) {
1920
size_t len = 0;
2021
mp_obj_t *items;
@@ -26,12 +27,8 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
2627
}
2728

2829
if (self->len < 2 * len) {
29-
if (self->points_list != NULL) {
30-
VECTORIO_POLYGON_DEBUG("free(%d), ", sizeof(self->points_list));
31-
gc_free(self->points_list);
32-
}
33-
self->points_list = gc_alloc(2 * len * sizeof(uint16_t), false, false);
34-
VECTORIO_POLYGON_DEBUG("alloc(%p, %d)", self->points_list, 2 * len * sizeof(uint16_t));
30+
self->points_list = gc_realloc(self->points_list, 2 * len * sizeof(uint16_t), true);
31+
VECTORIO_POLYGON_DEBUG("realloc(%d) -> %p", self->points_list, 2 * len * sizeof(uint16_t));
3532
}
3633
self->len = 2 * len;
3734

@@ -42,17 +39,10 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
4239

4340
mp_arg_validate_length(tuple_len, 2, MP_QSTR_point);
4441

45-
mp_int_t x;
46-
mp_int_t y;
47-
if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x)
48-
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)
49-
|| x < SHRT_MIN || x > SHRT_MAX || y < SHRT_MIN || y > SHRT_MAX
50-
) {
51-
gc_free(self->points_list);
52-
self->points_list = NULL;
53-
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
54-
self->len = 0;
55-
}
42+
mp_int_t x = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_x);
43+
mp_arg_validate_int_range(x, SHRT_MIN, SHRT_MAX, MP_QSTR_x);
44+
mp_int_t y = mp_arg_validate_type_int(tuple_items[1], MP_QSTR_y);
45+
mp_arg_validate_int_range(y, SHRT_MIN, SHRT_MAX, MP_QSTR_y);
5646
self->points_list[2 * i ] = (int16_t)x;
5747
self->points_list[2 * i + 1] = (int16_t)y;
5848
}

shared-module/vectorio/VectorShape.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,8 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self
277277
mp_obj_tuple_get(xy, &tuple_len, &tuple_items);
278278
mp_arg_validate_length(tuple_len, 2, MP_QSTR_location);
279279

280-
mp_int_t x;
281-
mp_int_t y;
282-
if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x)
283-
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) {
284-
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
285-
}
280+
mp_int_t x = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_x);
281+
mp_int_t y = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_y);
286282
bool dirty = false;
287283
if (self->x != x) {
288284
check_bounds_and_set_x(self, x);

0 commit comments

Comments
 (0)