Initialize all variables

This commit is contained in:
Max Bruckner 2016-11-13 18:05:44 +07:00
parent a5ff796c20
commit b88da9b0de
4 changed files with 55 additions and 55 deletions

64
cJSON.c
View File

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

View File

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

24
test.c
View File

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

View File

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