Skip to content

Commit f29de51

Browse files
committed
Check native object in case of early access
If a native displayio object is accessed before it's super().__init__() has been called, then a placeholder is given that will cause a crash if accessed. This is tricky to get right so we detect this case and raise a NotInplementedError instead of crashing. Fixes adafruit#1881
1 parent a6785d7 commit f29de51

File tree

8 files changed

+32
-12
lines changed

8 files changed

+32
-12
lines changed

py/objtype.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *class, const mp_obj_
117117
return o;
118118
}
119119

120+
// When instances are first created they have the base_init wrapper as their native parent's
121+
// instance because make_new combines __new__ and __init__. This object is invalid for the native
122+
// code so it must call this method to ensure that the given object has been __init__'d and is
123+
// valid.
124+
void mp_obj_assert_native_inited(mp_obj_t native_object) {
125+
if (native_object == MP_OBJ_FROM_PTR(&native_base_init_wrapper_obj)) {
126+
mp_raise_NotImplementedError(translate("Call super().__init__() before accessing native object."));
127+
}
128+
}
129+
120130
// TODO
121131
// This implements depth-first left-to-right MRO, which is not compliant with Python3 MRO
122132
// http://python-history.blogspot.com/2010/06/method-resolution-order.html

py/objtype.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ typedef struct _mp_obj_instance_t {
3737
// TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them
3838
} mp_obj_instance_t;
3939

40+
void mp_obj_assert_native_inited(mp_obj_t native_object);
41+
4042
#if MICROPY_CPYTHON_COMPAT
4143
// this is needed for object.__new__
4244
mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *cls, const mp_obj_type_t **native_base);

shared-bindings/_stage/__init__.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ STATIC mp_obj_t stage_render(size_t n_args, const mp_obj_t *args) {
8585

8686
mp_obj_t native_display = mp_instance_cast_to_native_base(args[6],
8787
&displayio_display_type);
88+
mp_obj_assert_native_inited(native_display);
8889
if (!MP_OBJ_IS_TYPE(native_display, &displayio_display_type)) {
8990
mp_raise_TypeError(translate("argument num/types mismatch"));
9091
}

shared-bindings/displayio/Display.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "lib/utils/context_manager_helpers.h"
3232
#include "py/binary.h"
3333
#include "py/objproperty.h"
34+
#include "py/objtype.h"
3435
#include "py/runtime.h"
3536
#include "shared-bindings/displayio/Group.h"
3637
#include "shared-bindings/microcontroller/Pin.h"
@@ -172,6 +173,7 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
172173
// Helper to ensure we have the native super class instead of a subclass.
173174
static displayio_display_obj_t* native_display(mp_obj_t display_obj) {
174175
mp_obj_t native_display = mp_instance_cast_to_native_base(display_obj, &displayio_display_type);
176+
mp_obj_assert_native_inited(native_display);
175177
return MP_OBJ_TO_PTR(native_display);
176178
}
177179

@@ -184,11 +186,7 @@ STATIC mp_obj_t displayio_display_obj_show(mp_obj_t self_in, mp_obj_t group_in)
184186
displayio_display_obj_t *self = native_display(self_in);
185187
displayio_group_t* group = NULL;
186188
if (group_in != mp_const_none) {
187-
mp_obj_t native_layer = mp_instance_cast_to_native_base(group_in, &displayio_group_type);
188-
if (native_layer == MP_OBJ_NULL) {
189-
mp_raise_ValueError(translate("Must be a Group subclass."));
190-
}
191-
group = MP_OBJ_TO_PTR(native_layer);
189+
group = MP_OBJ_TO_PTR(native_group(group_in));
192190
}
193191

194192
common_hal_displayio_display_show(self, group);

shared-bindings/displayio/Group.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "lib/utils/context_manager_helpers.h"
3232
#include "py/binary.h"
3333
#include "py/objproperty.h"
34+
#include "py/objtype.h"
3435
#include "py/runtime.h"
3536
#include "supervisor/shared/translate.h"
3637

@@ -80,8 +81,12 @@ STATIC mp_obj_t displayio_group_make_new(const mp_obj_type_t *type, size_t n_arg
8081
}
8182

8283
// Helper to ensure we have the native super class instead of a subclass.
83-
static displayio_group_t* native_group(mp_obj_t group_obj) {
84+
displayio_group_t* native_group(mp_obj_t group_obj) {
8485
mp_obj_t native_group = mp_instance_cast_to_native_base(group_obj, &displayio_group_type);
86+
if (native_group == MP_OBJ_NULL) {
87+
mp_raise_ValueError_varg(translate("Must be a %q subclass."), MP_QSTR_Group);
88+
}
89+
mp_obj_assert_native_inited(native_group);
8590
return MP_OBJ_TO_PTR(native_group);
8691
}
8792

shared-bindings/displayio/Group.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
extern const mp_obj_type_t displayio_group_type;
3333

34+
displayio_group_t* native_group(mp_obj_t group_obj);
3435

3536
void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y);
3637
uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self);

shared-bindings/displayio/TileGrid.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "lib/utils/context_manager_helpers.h"
3232
#include "py/binary.h"
3333
#include "py/objproperty.h"
34+
#include "py/objtype.h"
3435
#include "py/runtime.h"
3536
#include "shared-bindings/displayio/Bitmap.h"
3637
#include "shared-bindings/displayio/ColorConverter.h"
@@ -139,6 +140,7 @@ STATIC mp_obj_t displayio_tilegrid_make_new(const mp_obj_type_t *type, size_t n_
139140
// Helper to ensure we have the native super class instead of a subclass.
140141
static displayio_tilegrid_t* native_tilegrid(mp_obj_t tilegrid_obj) {
141142
mp_obj_t native_tilegrid = mp_instance_cast_to_native_base(tilegrid_obj, &displayio_tilegrid_type);
143+
mp_obj_assert_native_inited(native_tilegrid);
142144
return MP_OBJ_TO_PTR(native_tilegrid);
143145
}
144146

shared-bindings/socket/__init__.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ STATIC mp_int_t _socket_recv_into(mod_network_socket_obj_t *sock, byte *buf, mp_
257257
//| Reads some bytes from the connected remote address, writing
258258
//| into the provided buffer. If bufsize <= len(buffer) is given,
259259
//| a maximum of bufsize bytes will be read into the buffer. If no
260-
//| valid value is given for bufsize, the default is the length of
260+
//| valid value is given for bufsize, the default is the length of
261261
//| the given buffer.
262262
//|
263263
//| Suits sockets of type SOCK_STREAM
@@ -274,13 +274,14 @@ STATIC mp_obj_t socket_recv_into(size_t n_args, const mp_obj_t *args) {
274274
}
275275
mp_buffer_info_t bufinfo;
276276
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
277-
mp_int_t len;
277+
mp_int_t len = bufinfo.len;
278278
if (n_args == 3) {
279-
len = mp_obj_get_int(args[2]);
280-
}
281-
if (n_args == 2 || (size_t) len > bufinfo.len) {
282-
len = bufinfo.len;
279+
mp_int_t given_len = mp_obj_get_int(args[2]);
280+
if (given_len < len) {
281+
len = given_len;
282+
}
283283
}
284+
284285
mp_int_t ret = _socket_recv_into(self, (byte*)bufinfo.buf, len);
285286
return mp_obj_new_int_from_uint(ret);
286287
}

0 commit comments

Comments
 (0)