浏览代码

fix #55 memory leak in cJSON_ReplaceItemInObject

Max Bruckner 8 年之前
父节点
当前提交
0d10e279c8
共有 1 个文件被更改,包括 6 次插入0 次删除
  1. 6 0
      cJSON.c

+ 6 - 0
cJSON.c

@@ -1861,6 +1861,12 @@ void cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem
     }
     if(c)
     {
+        /* free the old string if not const */
+        if (!(newitem->type & cJSON_StringIsConst) && newitem->string)
+        {
+             cJSON_free(newitem->string);
+        }
+
         newitem->string = cJSON_strdup(string);
         cJSON_ReplaceItemInArray(object, i, newitem);
     }