Skip to content

Commit 83e2cf6

Browse files
mark-kubackinicowilliams
authored andcommitted
Skip printing what's below a MAX_PRINT_DEPTH
This addresses #1136, and mitigates a stack exhaustion when printing a very deeply nested term.
1 parent 61b7563 commit 83e2cf6

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/jv_print.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "jv_dtoa.h"
1414
#include "jv_unicode.h"
1515

16+
#ifndef MAX_PRINT_DEPTH
17+
#define MAX_PRINT_DEPTH (256)
18+
#endif
19+
1620
#define ESC "\033"
1721
#define COL(c) (ESC "[" c "m")
1822
#define COLRESET (ESC "[0m")
@@ -150,7 +154,9 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
150154
}
151155
}
152156
}
153-
switch (jv_get_kind(x)) {
157+
if (indent > MAX_PRINT_DEPTH) {
158+
put_str("<skipped: too deep>", F, S, flags & JV_PRINT_ISATTY);
159+
} else switch (jv_get_kind(x)) {
154160
default:
155161
case JV_KIND_INVALID:
156162
if (flags & JV_PRINT_INVALID) {

0 commit comments

Comments
 (0)