Permalink
Browse files

Initialize all variables

  • Loading branch information...
1 parent a5ff796 commit b88da9b0ded73bf63fd11e589b52d9cf1e95f7d8 @FSMaxB FSMaxB committed Nov 13, 2016
Showing with 55 additions and 55 deletions.
  1. +32 −32 cJSON.c
  2. +7 −7 cJSON_Utils.c
  3. +12 −12 test.c
  4. +4 −4 test_utils.c
View
@@ -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;
@ajaybhargav
ajaybhargav Nov 22, 2016 Contributor

Static variables are zeroed by default by initialization code, I see this a unnecessary.

@FSMaxB
FSMaxB Nov 22, 2016 Collaborator

It doesn't hurt, but makes it explicit to everybody who is reading the code.

@ajaybhargav
ajaybhargav Nov 22, 2016 Contributor

Ok! Not convinced though.

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)
View
@@ -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);
View
@@ -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] =
View
@@ -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;

4 comments on commit b88da9b

@ajaybhargav
Contributor

I do not see any usefulness of this patch at all. initializing local variables even when it is not needed adds unwanted code. Any thoughts?

@FSMaxB
Collaborator
FSMaxB commented on b88da9b Nov 22, 2016

It is a safe guard which prevents the use of uninitialized variables. In most cases where the variable is written to right after the declaration, a compiler will apply dead store elimination and not actually create code to initialize the variable. If however, the variable might be used in an uninitialized state, it is a good think to set it to some defined value.

@FSMaxB
Collaborator
FSMaxB commented on b88da9b Nov 22, 2016 edited

After I did that change, I compared the assembly that the compiler produced before and after the change, and it actually created more instructions, but I didn't look into it too much.

I should take a closer look at the places where no dead store eliminination took place, because that could in some cases indicate a bug.

@ajaybhargav
Contributor

Compiler indicate you with a following warning in case any variable is used without any value is assigned to it.
"warning: xxx may be used uninitialized in this function". Maybe looking for such warning might help?

Please sign in to comment.