aboutsummaryrefslogtreecommitdiff
path: root/cJSON.c
diff options
context:
space:
mode:
Diffstat (limited to 'cJSON.c')
-rw-r--r--cJSON.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/cJSON.c b/cJSON.c
index 56f65ef..9399c0d 100644
--- a/cJSON.c
+++ b/cJSON.c
@@ -2737,8 +2737,15 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
}
/* Duplication */
+cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse);
+
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
{
+ return cJSON_Duplicate_rec(item, 0, recurse );
+}
+
+cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse)
+{
cJSON *newitem = NULL;
cJSON *child = NULL;
cJSON *next = NULL;
@@ -2784,7 +2791,10 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
child = item->child;
while (child != NULL)
{
- newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
+ if(depth >= CJSON_CIRCULAR_LIMIT) {
+ goto fail;
+ }
+ newchild = cJSON_Duplicate_rec(child, ++depth, true); /* Duplicate (with recurse) each item in the ->next chain */
if (!newchild)
{
goto fail;