Skip to content

Commit d29973c

Browse files
committed
simplify logic in _clobber_points_list
1 parent 3af2564 commit d29973c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

shared-module/vectorio/Polygon.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
// Converts a list of points tuples to a flat list of ints for speedier internal use.
1717
// Also validates the points. If this fails due to invalid types or values, the
18-
// content of the points is undefined.
18+
// number of points is 0 and the points_list is NULL.
1919
static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple_list) {
2020
size_t len = 0;
2121
mp_obj_t *items;
@@ -26,11 +26,12 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
2626
mp_raise_TypeError(translate("Polygon needs at least 3 points"));
2727
}
2828

29-
if (self->len < 2 * len) {
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));
32-
}
33-
self->len = 2 * len;
29+
int16_t *points_list = gc_realloc(self->points_list, 2 * len * sizeof(uint16_t), true);
30+
VECTORIO_POLYGON_DEBUG("realloc(%p, %d) -> %p", self->points_list, 2 * len * sizeof(uint16_t), points_list);
31+
32+
// In case the validation calls below fail, set these values temporarily
33+
self->points_list = NULL;
34+
self->len = 0;
3435

3536
for (uint16_t i = 0; i < len; ++i) {
3637
size_t tuple_len = 0;
@@ -43,9 +44,12 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
4344
mp_arg_validate_int_range(x, SHRT_MIN, SHRT_MAX, MP_QSTR_x);
4445
mp_int_t y = mp_arg_validate_type_int(tuple_items[1], MP_QSTR_y);
4546
mp_arg_validate_int_range(y, SHRT_MIN, SHRT_MAX, MP_QSTR_y);
46-
self->points_list[2 * i ] = (int16_t)x;
47-
self->points_list[2 * i + 1] = (int16_t)y;
47+
points_list[2 * i ] = (int16_t)x;
48+
points_list[2 * i + 1] = (int16_t)y;
4849
}
50+
51+
self->points_list = points_list;
52+
self->len = 2 * len;
4953
}
5054

5155

0 commit comments

Comments
 (0)