8000 Improve the bounds checking on the location (the x, y co-ordinates) of a · urish/circuitpython@1f44691 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f44691

Browse files
Improve the bounds checking on the location (the x, y co-ordinates) of a
VectorShape object so that it is consistent no matter where it is set from: * the constructor * the x and y setters * the location setter
1 parent 0ee0ed7 commit 1f44691

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

shared-bindings/vectorio/Circle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg
4242

4343
// VectorShape parts
4444
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
45-
int16_t x = args[ARG_x].u_int;
46-
int16_t y = args[ARG_y].u_int;
45+
int32_t x = args[ARG_x].u_int;
46+
int32_t y = args[ARG_y].u_int;
4747
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
4848
self->draw_protocol_instance = vector_shape;
4949

shared-bindings/vectorio/Polygon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar
4747

4848
// VectorShape parts
4949
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
50-
int16_t x = args[ARG_x].u_int;
51-
int16_t y = args[ARG_y].u_int;
50+
int32_t x = args[ARG_x].u_int;
51+
int32_t y = args[ARG_y].u_int;
5252
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
5353
self->draw_protocol_instance = vector_shape;
5454

shared-bindings/vectorio/Rectangle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_
4646

4747
// VectorShape parts
4848
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
49-
int16_t x = args[ARG_x].u_int;
50-
int16_t y = args[ARG_y].u_int;
49+
int32_t x = args[ARG_x].u_int;
50+
int32_t y = args[ARG_y].u_int;
5151
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
5252
self->draw_protocol_instance = vector_shape;
5353

shared-bindings/vectorio/VectorShape.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// pixel_shader: The pixel shader that produces colors from values. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0.
2424
// x: Initial x position of the center axis of the shape within the parent.
2525
// y: Initial y position of the center axis of the shape within the parent."""
26-
mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int16_t x, int16_t y) {
26+
mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int32_t x, int32_t y) {
2727
if (!mp_obj_is_type(pixel_shader, &displayio_colorconverter_type) &&
2828
!mp_obj_is_type(pixel_shader, &displayio_palette_type)) {
2929
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_pixel_shader);
@@ -99,7 +99,10 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t wrapper_shape, mp_obj_t
9999
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
100100

101101
mp_int_t x = mp_obj_get_int(x_obj);
102-
common_hal_vectorio_vector_shape_set_x(self, x);
102+
bool dirty = common_hal_vectorio_vector_shape_set_x(self, x);
103+
if (dirty) {
104+
common_hal_vectorio_vector_shape_set_dirty(self);
105+
}
103106
return mp_const_none;
104107
}
105108
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_x_obj, vectorio_vector_shape_obj_set_x);
@@ -130,7 +133,10 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t wrapper_shape, mp_obj_t
130133
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
131134

132135
mp_int_t y = mp_obj_get_int(y_obj);
133-
common_hal_vectorio_vector_shape_set_y(self, y);
136+
bool dirty = common_hal_vectorio_vector_shape_set_y(self, y);
137+
if (dirty) {
138+
common_hal_vectorio_vector_shape_set_dirty(self);
139+
}
134140
return mp_const_none;
135141
}
136142
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_y_obj, vectorio_vector_shape_obj_set_y);

shared-bindings/vectorio/VectorShape.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
extern const mp_obj_type_t vectorio_vector_shape_type;
1212

1313
// Python shared bindings constructor
14-
mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int16_t x, int16_t y);
14+
mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pixel_shader, int32_t x, int32_t y);
1515

1616
// C data constructor
1717
void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
1818
vectorio_ishape_t ishape,
19-
mp_obj_t pixel_shader, uint16_t x, uint16_t y);
19+
mp_obj_t pixel_shader, int32_t x, int32_t y);
2020

2121
void common_hal_vectorio_vector_shape_set_dirty(void *self);
2222

2323
mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self);
24-
void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x);
24+
bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x);
2525

2626
mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self);
2727
void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self, mp_obj_t xy);
2828

2929
mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self);
30-
void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y);
30+
bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y);
3131

3232
mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self);
3333
void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader);

shared-module/vectorio/VectorShape.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ void common_hal_vectorio_vector_shape_set_dirty(void *vector_shape) {
164164

165165
void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
166166
vectorio_ishape_t ishape,
167-
mp_obj_t pixel_shader, uint16_t x, uint16_t y) {
167+
mp_obj_t pixel_shader, int32_t x, int32_t y) {
168168
VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y);
169-
self->x = x;
170-
self->y = y;
169+
common_hal_vectorio_vector_shape_set_x(self, x);
170+
common_hal_vectorio_vector_shape_set_y(self, y);
171171
self->pixel_shader = pixel_shader;
172172
self->ishape = ishape;
173173
self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area.
@@ -184,13 +184,16 @@ mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self) {
184184
}
185185

186186

187-
void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) {
187+
bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) {
188188
VECTORIO_SHAPE_DEBUG("%p set_x %d\n", self, x);
189189
if (self->x == x) {
190-
return;
190+
return false; // it's not dirty
191+
}
192+
if (x < SHRT_MIN || x > SHRT_MAX) {
193+
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
191194
}
192195
self->x = x;
193-
common_hal_vectorio_vector_shape_set_dirty(self);
196+
return true; // it's dirty
194197
}
195198

196199

@@ -200,13 +203,16 @@ mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self) {
200203
}
201204

202205

203-
void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) {
206+
bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) {
204207
VECTORIO_SHAPE_DEBUG("%p set_y %d\n", self, y);
205208
if (self->y == y) {
206-
return;
209+
return false; // it's not dirty
210+
}
211+
if (y < SHRT_MIN || y > SHRT_MAX) {
212+
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
207213
}
208214
self->y = y;
209-
common_hal_vectorio_vector_shape_set_dirty(self);
215+
return true; // it's dirty
210216
}
211217

212218
mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self) {
@@ -230,14 +236,14 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self
230236
mp_int_t x;
231237
mp_int_t y;
232238
if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x)
233-
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)
234-
|| x < SHRT_MIN || x > SHRT_MAX || y < SHRT_MIN || y > SHRT_MAX
235-
) {
239+
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) {
236240
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
237241
}
238-
self->x = (int16_t)x;
239-
self->y = (int16_t)y;
240-
common_hal_vectorio_vector_shape_set_dirty(self);
242+
bool dirty = common_hal_vectorio_vector_shape_set_x(self, x);
243+
dirty |= common_hal_vectorio_vector_shape_set_y(self, y);
244+
if (dirty) {
245+
common_hal_vectorio_vector_shape_set_dirty(self);
246+
}
241247
}
242248

243249

0 commit comments

Comments
 (0)
0