Browse Source

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
Dave Gamble 12 năm trước cách đây
mục cha
commit
d755436ba7
1 tập tin đã thay đổi với 3 bổ sung2 xóa
  1. 3 2
      cJSON.c

+ 3 - 2
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;