From d755436ba727b14ec74bf7ef6256b332b715c679 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Tue, 5 Feb 2013 18:26:51 +0000 Subject: [PATCH] tests for cJSON_Duplicate, so it will fail by returning 0 if anything fails to allocate git-svn-id: http://svn.code.sf.net/p/cjson/code@49 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index c56d44a..ff5bd85 100644 --- a/cJSON.c +++ b/cJSON.c @@ -541,8 +541,8 @@ cJSON *cJSON_Duplicate(cJSON *item,int recurse) if (!newitem) return 0; /* Copy over all vars */ newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble; - if (item->valuestring) newitem->valuestring=cJSON_strdup(item->valuestring); - if (item->string) newitem->string=cJSON_strdup(item->string); + if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}} + if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}} /* If non-recursive, then we're done! */ if (!recurse) return newitem; /* Walk the ->next chain for the child. */ @@ -550,6 +550,7 @@ cJSON *cJSON_Duplicate(cJSON *item,int recurse) while (cptr) { newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) {cJSON_Delete(newitem);return 0;} if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */ else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */ cptr=cptr->next;