8000 Fix several places where an exception could be chained wrongly · vickash/circuitpython@0d957fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d957fe

Browse files
committed
Fix several places where an exception could be chained wrongly
If an exception's chain or context can refer to a pointer from a different VM, a crash would typically result. This couldn't turn up on UNIX testing because the VM is never torn down and rebuilt like it is on hardware. Because in the 'static' case the GeneratorObject is now fully initialized whenever it's raised, the initialization can be dropped, which reduces the flash size slightly. Closes: adafruit#7565
1 parent fc919d2 commit 0d957fe

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

py/objexcept.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kin
155155
mp_obj_tuple_print(print, MP_OBJ_FROM_PTR(o->args), kind);
156156
}
157157

158+
void mp_obj_exception_initialize0(mp_obj_exception_t *o_exc, const mp_obj_type_t *type) {
159+
o_exc->base.type = type;
160+
o_exc->args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;
161+
mp_obj_exception_clear_traceback(o_exc);
162+
}
163+
158164
mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
159165
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
160166

@@ -583,6 +589,12 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
583589
// just set the traceback to the empty traceback object
584590
// we don't want to call any memory management functions here
585591
self->traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
592+
#if MICROPY_CPYTHON_EXCEPTION_CHAIN
593+
self->cause = 0;
594+
self->context = 0;
595+
self->suppress_context = false;
596+
self->marked = false;
597+
#endif
586598
}
587599

588600
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block) {

py/objexcept.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct _mp_obj_exception_t {
4343

4444
void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
4545
void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
46+
void mp_obj_exception_initialize0(mp_obj_exception_t *o_exc, const mp_obj_type_t *type);
4647
mp_obj_exception_t *mp_obj_exception_get_native(mp_obj_t self_in);
4748

4849
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \

py/objgenerator.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
// Instance of GeneratorExit exception - needed by generator.close()
4141
#if MICROPY_CONST_GENERATOREXIT_OBJ
4242
const
43+
mp_obj_exception_t mp_static_GeneratorExit_obj = {{&mp_type_GeneratorExit}, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj, (mp_obj_traceback_t *)&mp_const_empty_traceback_obj};
4344
#else
4445
static
46+
mp_obj_exception_t mp_static_GeneratorExit_obj;
4547
#endif
46-
mp_obj_exception_t mp_static_GeneratorExit_obj = {{&mp_type_GeneratorExit}, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj, (mp_obj_traceback_t *)&mp_const_empty_traceback_obj};
4748

4849
/******************************************************************************/
4950
/* generator wrapper */
@@ -370,9 +371,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_ins
370371
static mp_obj_t generatorexit(void) {
371372
#if MICROPY_CPYTHON_EXCEPTION_CHAIN
372373
MP_STATIC_ASSERT(!MICROPY_CONST_GENERATOREXIT_OBJ);
373-
mp_static_GeneratorExit_obj.context = NULL;
374-
mp_static_GeneratorExit_obj.cause = NULL;
375-
mp_static_GeneratorExit_obj.suppress_context = false;
374+
mp_obj_exception_initialize0(&mp_static_GeneratorExit_obj, &mp_type_GeneratorExit);
376375
#endif
377376
return MP_OBJ_FROM_PTR(&mp_static_GeneratorExit_obj);
378377
}

py/runtime.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,10 @@ void mp_init(void) {
7878

7979
#if MICROPY_KBD_EXCEPTION
8080
// initialise the exception object for raising KeyboardInterrupt
81-
MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_KeyboardInterrupt;
82-
MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;
83-
MP_STATE_VM(mp_kbd_exception).traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
81+
mp_obj_exception_initialize0(&MP_STATE_VM(mp_kbd_exception), &mp_type_KeyboardInterrupt);
8482
#endif
8583

86-
MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException;
87-
MP_STATE_VM(mp_reload_exception).args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;
88-
MP_STATE_VM(mp_reload_exception).traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
84+
mp_obj_exception_initialize0(&MP_STATE_VM(mp_reload_exception), &mp_type_ReloadException);
8985

9086
// call port specific initialization if any
9187
#ifdef MICROPY_PORT_INIT_FUNC

0 commit comments

Comments
 (0)
0