瀏覽代碼

Initialize all variables

Max Bruckner 8 年之前
父節點
當前提交
b88da9b0de
共有 4 個文件被更改,包括 55 次插入55 次删除
  1. 32 32
      cJSON.c
  2. 7 7
      cJSON_Utils.c
  3. 12 12
      test.c
  4. 4 4
      test_utils.c

+ 32 - 32
cJSON.c

@@ -46,7 +46,7 @@
     #error "Failed to determine the size of an integer"
 #endif
 
-static const char *global_ep;
+static const char *global_ep = NULL;
 
 const char *cJSON_GetErrorPtr(void)
 {
@@ -80,8 +80,8 @@ static void (*cJSON_free)(void *ptr) = free;
 
 static char* cJSON_strdup(const char* str)
 {
-    size_t len;
-    char* copy;
+    size_t len = 0;
+    char *copy = NULL;
 
     len = strlen(str) + 1;
     if (!(copy = (char*)cJSON_malloc(len)))
@@ -122,7 +122,7 @@ static cJSON *cJSON_New_Item(void)
 /* Delete a cJSON structure. */
 void cJSON_Delete(cJSON *c)
 {
-    cJSON *next;
+    cJSON *next = NULL;
     while (c)
     {
         next = c->next;
@@ -244,8 +244,8 @@ typedef struct
 /* realloc printbuffer if necessary to have at least "needed" bytes more */
 static char* ensure(printbuffer *p, int needed)
 {
-    char *newbuffer;
-    int newsize;
+    char *newbuffer = NULL;
+    int newsize = 0;
     if (!p || !p->buffer)
     {
         return NULL;
@@ -280,7 +280,7 @@ static char* ensure(printbuffer *p, int needed)
 /* calculate the new length of the string in a printbuffer */
 static int update(const printbuffer *p)
 {
-    char *str;
+    char *str = NULL;
     if (!p || !p->buffer)
     {
         return 0;
@@ -468,11 +468,11 @@ static const char *parse_string(cJSON *item, const char *str, const char **ep)
 {
     const char *ptr = str + 1;
     const char *end_ptr =str + 1;
-    char *ptr2;
-    char *out;
+    char *ptr2 = NULL;
+    char *out = NULL;
     int len = 0;
-    unsigned uc;
-    unsigned uc2;
+    unsigned uc = 0;
+    unsigned uc2 = 0;
 
     /* not a string! */
     if (*str != '\"')
@@ -643,12 +643,12 @@ static const char *parse_string(cJSON *item, const char *str, const char **ep)
 /* Render the cstring provided to an escaped version that can be printed. */
 static char *print_string_ptr(const char *str, printbuffer *p)
 {
-    const char *ptr;
-    char *ptr2;
-    char *out;
+    const char *ptr = NULL;
+    char *ptr2 = NULL;
+    char *out = NULL;
     int len = 0;
     int flag = 0;
-    unsigned char token;
+    unsigned char token = '\0';
 
     /* empty string */
     if (!str)
@@ -1012,7 +1012,7 @@ static char *print_value(const cJSON *item, int depth, int fmt, printbuffer *p)
 /* Build an array from input text. */
 static const char *parse_array(cJSON *item,const char *value,const char **ep)
 {
-    cJSON *child;
+    cJSON *child = NULL;
     if (*value != '[')
     {
         /* not an array! */
@@ -1044,7 +1044,7 @@ static const char *parse_array(cJSON *item,const char *value,const char **ep)
     /* loop through the comma separated array elements */
     while (*value == ',')
     {
-        cJSON *new_item;
+        cJSON *new_item = NULL;
         if (!(new_item = cJSON_New_Item()))
         {
             /* memory fail */
@@ -1081,8 +1081,8 @@ static char *print_array(const cJSON *item, int depth, int fmt, printbuffer *p)
 {
     char **entries;
     char *out = NULL;
-    char *ptr;
-    char *ret;
+    char *ptr = NULL;
+    char *ret = NULL;
     int len = 5;
     cJSON *child = item->child;
     int numentries = 0;
@@ -1245,7 +1245,7 @@ static char *print_array(const cJSON *item, int depth, int fmt, printbuffer *p)
 /* Build an object from the text. */
 static const char *parse_object(cJSON *item, const char *value, const char **ep)
 {
-    cJSON *child;
+    cJSON *child = NULL;
     if (*value != '{')
     {
         /* not an object! */
@@ -1292,7 +1292,7 @@ static const char *parse_object(cJSON *item, const char *value, const char **ep)
 
     while (*value == ',')
     {
-        cJSON *new_item;
+        cJSON *new_item = NULL;
         if (!(new_item = cJSON_New_Item()))
         {
             /* memory fail */
@@ -1343,12 +1343,12 @@ static char *print_object(const cJSON *item, int depth, int fmt, printbuffer *p)
     char **entries = NULL;
     char **names = NULL;
     char *out = NULL;
-    char *ptr;
-    char *ret;
-    char *str;
+    char *ptr = NULL;
+    char *ret = NULL;
+    char *str = NULL;
     int len = 7;
     int i = 0;
-    int j;
+    int j = 0;
     cJSON *child = item->child;
     int numentries = 0;
     int fail = 0;
@@ -1979,7 +1979,7 @@ cJSON *cJSON_CreateObject(void)
 /* Create Arrays: */
 cJSON *cJSON_CreateIntArray(const int *numbers, int count)
 {
-    int i;
+    int i = 0;
     cJSON *n = NULL;
     cJSON *p = NULL;
     cJSON *a = cJSON_CreateArray();
@@ -2007,7 +2007,7 @@ cJSON *cJSON_CreateIntArray(const int *numbers, int count)
 
 cJSON *cJSON_CreateFloatArray(const float *numbers, int count)
 {
-    int i;
+    int i = 0;
     cJSON *n = NULL;
     cJSON *p = NULL;
     cJSON *a = cJSON_CreateArray();
@@ -2035,7 +2035,7 @@ cJSON *cJSON_CreateFloatArray(const float *numbers, int count)
 
 cJSON *cJSON_CreateDoubleArray(const double *numbers, int count)
 {
-    int i;
+    int i = 0;
     cJSON *n = NULL;
     cJSON *p = NULL;
     cJSON *a = cJSON_CreateArray();
@@ -2063,7 +2063,7 @@ cJSON *cJSON_CreateDoubleArray(const double *numbers, int count)
 
 cJSON *cJSON_CreateStringArray(const char **strings, int count)
 {
-    int i;
+    int i = 0;
     cJSON *n = NULL;
     cJSON *p = NULL;
     cJSON *a = cJSON_CreateArray();
@@ -2092,10 +2092,10 @@ cJSON *cJSON_CreateStringArray(const char **strings, int count)
 /* Duplication */
 cJSON *cJSON_Duplicate(const cJSON *item, int recurse)
 {
-    cJSON *newitem;
-    cJSON *cptr;
+    cJSON *newitem = NULL;
+    cJSON *cptr = NULL;
     cJSON *nptr = NULL;
-    cJSON *newchild;
+    cJSON *newchild = NULL;
 
     /* Bail on bad ptr */
     if (!item)

+ 7 - 7
cJSON_Utils.c

@@ -6,8 +6,8 @@
 
 static char* cJSONUtils_strdup(const char* str)
 {
-    size_t len;
-    char* copy;
+    size_t len = 0;
+    char *copy = NULL;
 
     len = strlen(str) + 1;
     if (!(copy = (char*)malloc(len)))
@@ -290,7 +290,7 @@ static int cJSONUtils_Compare(cJSON *a, cJSON *b)
             b = b->child;
             while (a && b)
             {
-                int err;
+                int err = 0;
                 /* compare object keys */
                 if (cJSONUtils_strcasecmp(a->string, b->string))
                 {
@@ -475,7 +475,7 @@ static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
 
 int cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches)
 {
-    int err;
+    int err = 0;
     if ((patches->type & 0xFF) != cJSON_Array)
     {
         /* malformed patches. */
@@ -550,7 +550,7 @@ static void cJSONUtils_CompareToPatch(cJSON *patches, const char *path, cJSON *f
 
         case cJSON_Array:
         {
-            int c;
+            int c = 0;
             char *newpath = (char*)malloc(strlen(path) + 23); /* Allow space for 64bit int. */
             /* generate patches for all array elements that exist in "from" and "to" */
             for (c = 0, from = from->child, to = to->child; from && to; from = from->next, to = to->next, c++)
@@ -575,8 +575,8 @@ static void cJSONUtils_CompareToPatch(cJSON *patches, const char *path, cJSON *f
 
         case cJSON_Object:
         {
-            cJSON *a;
-            cJSON *b;
+            cJSON *a = NULL;
+            cJSON *b = NULL;
             cJSONUtils_SortObject(from);
             cJSONUtils_SortObject(to);
 

+ 12 - 12
test.c

@@ -27,8 +27,8 @@
 /* Parse text to JSON, then render back to text, and print! */
 void doit(char *text)
 {
-    char *out;
-    cJSON *json;
+    char *out = NULL;
+    cJSON *json = NULL;
 
     json = cJSON_Parse(text);
     if (!json)
@@ -47,9 +47,9 @@ void doit(char *text)
 /* Read a file, parse, render back, etc. */
 void dofile(char *filename)
 {
-    FILE *f;
-    long len;
-    char *data;
+    FILE *f = NULL;
+    long len = 0;
+    char *data = NULL;
 
     /* open in read binary mode */
     f = fopen(filename,"rb");
@@ -85,13 +85,13 @@ struct record
 void create_objects(void)
 {
     /* declare a few. */
-    cJSON *root;
-    cJSON *fmt;
-    cJSON *img;
-    cJSON *thm;
-    cJSON *fld;
-    char *out;
-    int i;
+    cJSON *root = NULL;
+    cJSON *fmt = NULL;
+    cJSON *img = NULL;
+    cJSON *thm = NULL;
+    cJSON *fld = NULL;
+    char *out = NULL;
+    int i = 0;
 
     /* Our "days of the week" array: */
     const char *strings[7] =

+ 4 - 4
test_utils.c

@@ -10,9 +10,9 @@ int main(void)
 	char *patchtext = NULL;
 	char *patchedtext = NULL;
 
-	int i;
+	int i = 0;
 	/* JSON Pointer tests: */
-	cJSON *root;
+	cJSON *root = NULL;
 	const char *json="{"
 		"\"foo\": [\"bar\", \"baz\"],"
 		"\"\": 0,"
@@ -68,8 +68,8 @@ int main(void)
 	/* Misc tests */
 	int numbers[10]={0,1,2,3,4,5,6,7,8,9};
 	const char *random="QWERTYUIOPASDFGHJKLZXCVBNM";
-	char buf[2]={0,0},*before,*after;
-	cJSON *object,*nums,*num6,*sortme;
+	char buf[2]={0,0}, *before = NULL,*after = NULL;
+	cJSON *object = NULL, *nums = NULL, *num6 = NULL, *sortme = NULL;