py: Add protection against printing too nested or recursive data structures.
With a test which cannot be automatically validated so far.nlr-macros
parent
7e4ec3bf4f
commit
8993fb6cf0
3
py/obj.c
3
py/obj.c
|
@ -35,6 +35,7 @@
|
||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
#include "runtime0.h"
|
#include "runtime0.h"
|
||||||
#include "runtime.h"
|
#include "runtime.h"
|
||||||
|
#include "stackctrl.h"
|
||||||
|
|
||||||
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
|
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
|
||||||
if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
||||||
|
@ -59,6 +60,8 @@ void printf_wrapper(void *env, const char *fmt, ...) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
|
// There can be data structures nested too deep, or just recursive
|
||||||
|
STACK_CHECK();
|
||||||
#if !NDEBUG
|
#if !NDEBUG
|
||||||
if (o_in == NULL) {
|
if (o_in == NULL) {
|
||||||
print(env, "(nil)");
|
print(env, "(nil)");
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
# This tests that printing recursive data structure doesn't lead to segfault.
|
||||||
|
# Unfortunately, print() so far doesn't support "file "kwarg, so variable-len
|
||||||
|
# output of this test cannot be redirected, and this test cannot be validated.
|
||||||
|
l = [1, 2, 3, None]
|
||||||
|
l[-1] = l
|
||||||
|
try:
|
||||||
|
print(l)
|
||||||
|
except RuntimeError:
|
||||||
|
print("RuntimeError")
|
Loading…
Reference in New Issue