Skip to content

Commit 8993fb6

Browse files
committed
py: Add protection against printing too nested or recursive data structures.
With a test which cannot be automatically validated so far.
1 parent 7e4ec3b commit 8993fb6

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

py/obj.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "obj.h"
3636
#include "runtime0.h"
3737
#include "runtime.h"
38+
#include "stackctrl.h"
3839

3940
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
4041
if (MP_OBJ_IS_SMALL_INT(o_in)) {
@@ -59,6 +60,8 @@ void printf_wrapper(void *env, const char *fmt, ...) {
5960
}
6061

6162
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
63+
// There can be data structures nested too deep, or just recursive
64+
STACK_CHECK();
6265
#if !NDEBUG
6366
if (o_in == NULL) {
6467
print(env, "(nil)");

tests/misc/recursive_data.py_

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This tests that printing recursive data structure doesn't lead to segfault.
2+
# Unfortunately, print() so far doesn't support "file "kwarg, so variable-len
3+
# output of this test cannot be redirected, and this test cannot be validated.
4+
l = [1, 2, 3, None]
5+
l[-1] = l
6+
try:
7+
print(l)
8+
except RuntimeError:
9+
print("RuntimeError")

0 commit comments

Comments
 (0)