From c5e788e2b258085c77de7c5f018339b763d3020f Mon Sep 17 00:00:00 2001 From: lilol <1258447103@qq.com> Date: Mon, 2 Feb 2026 10:48:10 +0800 Subject: [PATCH] Fix: add depth check to prevent stack overflow in cJSON_Print --- cJSON.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cJSON.c b/cJSON.c index 6e4fb0dd..f16a7bcc 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1598,6 +1598,11 @@ static cJSON_bool print_array(const cJSON * const item, printbuffer * const outp return false; } + if (output_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* nesting is too deep */ + } + /* Compose the output array. */ /* opening square bracket */ output_pointer = ensure(output_buffer, 1); @@ -1778,6 +1783,11 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out return false; } + if (output_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* nesting is too deep */ + } + /* Compose the output: */ length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ output_pointer = ensure(output_buffer, length + 1);