From 991b57724766c7a19ff72b8a603af53c3bb93fdd Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Tue, 10 Nov 2009 23:50:43 +0000 Subject: [PATCH] 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 --- cJSON.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cJSON.c b/cJSON.c index e6df5a7..28a50ba 100644 --- a/cJSON.c +++ b/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;}