Skip to content

Commit

Permalink
tests for cJSON_Duplicate, so it will fail by returning 0 if anything…
Browse files Browse the repository at this point in the history
… fails to allocate

git-svn-id: http://svn.code.sf.net/p/cjson/code@49 e3330c51-1366-4df0-8b21-3ccf24e3d50e
  • Loading branch information
davegamble committed Feb 5, 2013
1 parent cba1d12 commit fd122df
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 541,16 @@ 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. */
cptr=item->child;
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;
Expand Down

0 comments on commit fd122df

Please sign in to comment.