15
15
16
16
// Converts a list of points tuples to a flat list of ints for speedier internal use.
17
17
// 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 .
19
19
static void _clobber_points_list (vectorio_polygon_t * self , mp_obj_t points_tuple_list ) {
20
20
size_t len = 0 ;
21
21
mp_obj_t * items ;
@@ -26,11 +26,12 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
26
26
mp_raise_TypeError (translate ("Polygon needs at least 3 points" ));
27
27
}
28
28
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 ;
34
35
35
36
for (uint16_t i = 0 ; i < len ; ++ i ) {
36
37
size_t tuple_len = 0 ;
@@ -43,9 +44,12 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
43
44
mp_arg_validate_int_range (x , SHRT_MIN , SHRT_MAX , MP_QSTR_x );
44
45
mp_int_t y = mp_arg_validate_type_int (tuple_items [1 ], MP_QSTR_y );
45
46
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 ;
48
49
}
50
+
51
+ self -> points_list = points_list ;
52
+ self -> len = 2 * len ;
49
53
}
50
54
51
55
0 commit comments