14
14
15
15
16
16
// 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
+ // number of points is 0 and the points_list is NULL.
18
19
static void _clobber_points_list (vectorio_polygon_t * self , mp_obj_t points_tuple_list ) {
19
20
size_t len = 0 ;
20
21
mp_obj_t * items ;
@@ -25,15 +26,12 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
25
26
mp_raise_TypeError (translate ("Polygon needs at least 3 points" ));
26
27
}
27
28
28
- 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 ));
35
- }
36
- 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 ;
37
35
38
36
for (uint16_t i = 0 ; i < len ; ++ i ) {
39
37
size_t tuple_len = 0 ;
@@ -42,20 +40,16 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
42
40
43
41
mp_arg_validate_length (tuple_len , 2 , MP_QSTR_point );
44
42
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
- }
56
- self -> points_list [2 * i ] = (int16_t )x ;
57
- self -> points_list [2 * i + 1 ] = (int16_t )y ;
43
+ mp_int_t x = mp_arg_validate_type_int (tuple_items [0 ], MP_QSTR_x );
44
+ mp_arg_validate_int_range (x , SHRT_MIN , SHRT_MAX , MP_QSTR_x );
45
+ mp_int_t y = mp_arg_validate_type_int (tuple_items [1 ], MP_QSTR_y );
46
+ mp_arg_validate_int_range (y , SHRT_MIN , SHRT_MAX , MP_QSTR_y );
47
+ points_list [2 * i ] = (int16_t )x ;
48
+ points_list [2 * i + 1 ] = (int16_t )y ;
58
49
}
50
+
51
+ self -> points_list = points_list ;
52
+ self -> len = 2 * len ;
59
53
}
60
54
61
55
0 commit comments