Procházet zdrojové kódy

fix the ReplaceObject functions; tighten them up, add some more checks for stability.

git-svn-id: http://svn.code.sf.net/p/cjson/code@14 e3330c51-1366-4df0-8b21-3ccf24e3d50e
Dave Gamble před 15 roky
rodič
revize
991b577247
1 změnil soubory, kde provedl 4 přidání a 3 odebrání
  1. 4 3
      cJSON.c

+ 4 - 3
cJSON.c

@@ -180,6 +180,7 @@ static char *print_string_ptr(const char *str)
 {
 	const char *ptr;char *ptr2,*out;int len=0;
 	
+	if (!str) return cJSON_strdup("");
 	ptr=str;while (*ptr && ++len) {if (*ptr<32 || *ptr=='\"' || *ptr=='\\') len++;ptr++;}
 	
 	out=(char*)cJSON_malloc(len+3);
@@ -440,7 +441,7 @@ static char *print_object(cJSON *item,int depth)
 
 // Get Array size/item / object item.
 int    cJSON_GetArraySize(cJSON *array)							{cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
-cJSON *cJSON_GetArrayItem(cJSON *array,int item)				{cJSON *c=array->child;  while (c && item) item--,c=c->next; return c;}
+cJSON *cJSON_GetArrayItem(cJSON *array,int item)				{cJSON *c=array->child;  while (c && item>0) item--,c=c->next; return c;}
 cJSON *cJSON_GetObjectItem(cJSON *object,const char *string)	{cJSON *c=object->child; while (c && strcasecmp(c->string,string)) c=c->next; return c;}
 
 // Utility for array list handling.
@@ -453,8 +454,8 @@ void   cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item)	{if (
 // Replace array/object items with new ones.
 void   cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem)		{cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
 	newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
-	if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;cJSON_Delete(c);}
-void   cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && strcasecmp(c->string,string))i++,c=c->next;if(c)cJSON_ReplaceItemInArray(object,i,newitem);}
+	if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
+void   cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
 
 // Create basic types:
 cJSON *cJSON_CreateNull()						{cJSON *item=cJSON_New_Item();item->type=cJSON_NULL;return item;}