Skip to content

Commit 9d1b229

Browse files
vwvwAlanscut
authored andcommitted
Added max recusrion depth for cJSONDuplicate to prevent stack exhaustion in case of circular reference
1 parent 078c4e6 commit 9d1b229

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

cJSON.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2737,7 +2737,14 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
27372737
}
27382738

27392739
/* Duplication */
2740+
cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse);
2741+
27402742
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2743+
{
2744+
return cJSON_Duplicate_rec(item, 0, recurse );
2745+
}
2746+
2747+
cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse)
27412748
{
27422749
cJSON *newitem = NULL;
27432750
cJSON *child = NULL;
@@ -2784,7 +2791,10 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
27842791
child = item->child;
27852792
while (child != NULL)
27862793
{
2787-
newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
2794+
if(depth >= CJSON_CIRCULAR_LIMIT) {
2795+
goto fail;
2796+
}
2797+
newchild = cJSON_Duplicate_rec(child, ++depth, true); /* Duplicate (with recurse) each item in the ->next chain */
27882798
if (!newchild)
27892799
{
27902800
goto fail;

cJSON.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ typedef int cJSON_bool;
137137
#define CJSON_NESTING_LIMIT 1000
138138
#endif
139139

140+
/* Limits the length of circular references can be before cJSON rejects to parse them.
141+
* This is to prevent stack overflows. */
142+
#ifndef CJSON_CIRCULAR_LIMIT
143+
#define CJSON_CIRCULAR_LIMIT 10000
144+
#endif
145+
140146
/* returns the version of cJSON as a string */
141147
CJSON_PUBLIC(const char*) cJSON_Version(void);
142148

tests/misc_tests.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,23 @@ static void cjson_should_not_parse_to_deeply_nested_jsons(void)
219219
TEST_ASSERT_NULL_MESSAGE(cJSON_Parse(deep_json), "To deep JSONs should not be parsed.");
220220
}
221221

222+
static void cjson_should_not_follow_too_deep_circular_references(void)
223+
{
224+
cJSON *o = cJSON_CreateArray();
225+
cJSON *a = cJSON_CreateArray();
226+
cJSON *b = cJSON_CreateArray();
227+
cJSON *x;
228+
229+
cJSON_AddItemToArray(o, a);
230+
cJSON_AddItemToArray(a, b);
231+
cJSON_AddItemToArray(b, o);
232+
233+
x = cJSON_Duplicate(o, 1);
234+
TEST_ASSERT_NULL(x);
235+
cJSON_DetachItemFromArray(b, 0);
236+
cJSON_Delete(o);
237+
}
238+
222239
static void cjson_set_number_value_should_set_numbers(void)
223240
{
224241
cJSON number[1] = {{NULL, NULL, NULL, cJSON_Number, NULL, 0, 0, NULL}};
@@ -777,6 +794,7 @@ int CJSON_CDECL main(void)
777794
RUN_TEST(cjson_get_object_item_case_sensitive_should_not_crash_with_array);
778795
RUN_TEST(typecheck_functions_should_check_type);
779796
RUN_TEST(cjson_should_not_parse_to_deeply_nested_jsons);
797+
RUN_TEST(cjson_should_not_follow_too_deep_circular_references);
780798
RUN_TEST(cjson_set_number_value_should_set_numbers);
781799
RUN_TEST(cjson_detach_item_via_pointer_should_detach_items);
782800
RUN_TEST(cjson_detach_item_via_pointer_should_return_null_if_item_prev_is_null);

0 commit comments

Comments
 (0)