mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Initialize all variables
This commit is contained in:
54
cJSON.c
54
cJSON.c
@@ -32,7 +32,7 @@
|
||||
#include <ctype.h>
|
||||
#include "cJSON.h"
|
||||
|
||||
static const char *global_ep;
|
||||
static const char *global_ep = NULL;
|
||||
|
||||
const char *cJSON_GetErrorPtr(void)
|
||||
{
|
||||
@@ -65,8 +65,8 @@ static cJSON_Hooks global_hooks = { malloc, free };
|
||||
|
||||
static char* cJSON_strdup(const char* str, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
size_t len;
|
||||
char* copy;
|
||||
size_t len = 0;
|
||||
char *copy = NULL;
|
||||
|
||||
len = strlen(str) + 1;
|
||||
if (!(copy = (char*)hooks->malloc_fn(len)))
|
||||
@@ -95,7 +95,7 @@ void cJSON_InitHooks(cJSON_Hooks* hooks)
|
||||
/* Internal constructor. */
|
||||
static cJSON *cJSON_New_Item(const cJSON_Hooks * const hooks)
|
||||
{
|
||||
cJSON* node = (cJSON*)hooks->malloc_fn(sizeof(cJSON));
|
||||
cJSON *node = (cJSON*)hooks->malloc_fn(sizeof(cJSON));
|
||||
if (node)
|
||||
{
|
||||
memset(node, '\0', sizeof(cJSON));
|
||||
@@ -107,7 +107,7 @@ static cJSON *cJSON_New_Item(const cJSON_Hooks * const hooks)
|
||||
/* Delete a cJSON structure. */
|
||||
void internal_cJSON_Delete(cJSON *c, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
cJSON *next;
|
||||
cJSON *next = NULL;
|
||||
while (c)
|
||||
{
|
||||
next = c->next;
|
||||
@@ -215,8 +215,8 @@ typedef struct
|
||||
/* realloc printbuffer if necessary to have at least "needed" bytes more */
|
||||
static char* ensure(printbuffer *p, size_t needed, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
char *newbuffer;
|
||||
size_t newsize;
|
||||
char *newbuffer = NULL;
|
||||
size_t newsize = 0;
|
||||
if (!p || !p->buffer)
|
||||
{
|
||||
return NULL;
|
||||
@@ -255,7 +255,7 @@ static char* ensure(printbuffer *p, size_t needed, const cJSON_Hooks * const hoo
|
||||
/* calculate the new length of the string in a printbuffer */
|
||||
static size_t update(const printbuffer *p)
|
||||
{
|
||||
char *str;
|
||||
char *str = NULL;
|
||||
if (!p || !p->buffer)
|
||||
{
|
||||
return 0;
|
||||
@@ -443,8 +443,8 @@ static const char *parse_string(cJSON *item, const char *str, const char **ep, c
|
||||
{
|
||||
const char *ptr = str + 1;
|
||||
const char *end_ptr =str + 1;
|
||||
char *ptr2;
|
||||
char *out;
|
||||
char *ptr2 = NULL;
|
||||
char *out = NULL;
|
||||
size_t len = 0;
|
||||
unsigned uc;
|
||||
unsigned uc2;
|
||||
@@ -613,9 +613,9 @@ static const char *parse_string(cJSON *item, const char *str, const char **ep, c
|
||||
/* Render the cstring provided to an escaped version that can be printed. */
|
||||
static char *print_string_ptr(const char *str, printbuffer *p, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
const char *ptr;
|
||||
char *ptr2;
|
||||
char *out;
|
||||
const char *ptr = NULL;
|
||||
char *ptr2 = NULL;
|
||||
char *out = NULL;
|
||||
size_t len = 0;
|
||||
int flag = 0;
|
||||
unsigned char token;
|
||||
@@ -984,7 +984,7 @@ static char *print_value(const cJSON *item, size_t depth, int fmt, printbuffer *
|
||||
/* Build an array from input text. */
|
||||
static const char *parse_array(cJSON *item,const char *value,const char **ep, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
cJSON *child;
|
||||
cJSON *child = NULL;
|
||||
if (*value != '[')
|
||||
{
|
||||
/* not an array! */
|
||||
@@ -1016,7 +1016,7 @@ static const char *parse_array(cJSON *item,const char *value,const char **ep, co
|
||||
/* loop through the comma separated array elements */
|
||||
while (*value == ',')
|
||||
{
|
||||
cJSON *new_item;
|
||||
cJSON *new_item = NULL;
|
||||
if (!(new_item = cJSON_New_Item(hooks)))
|
||||
{
|
||||
/* memory fail */
|
||||
@@ -1053,8 +1053,8 @@ static char *print_array(const cJSON *item, size_t depth, int fmt, printbuffer *
|
||||
{
|
||||
char **entries;
|
||||
char *out = NULL;
|
||||
char *ptr;
|
||||
char *ret;
|
||||
char *ptr = NULL;
|
||||
char *ret = NULL;
|
||||
size_t len = 5;
|
||||
cJSON *child = item->child;
|
||||
size_t numentries = 0;
|
||||
@@ -1217,7 +1217,7 @@ static char *print_array(const cJSON *item, size_t depth, int fmt, printbuffer *
|
||||
/* Build an object from the text. */
|
||||
static const char *parse_object(cJSON *item, const char *value, const char **ep, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
cJSON *child;
|
||||
cJSON *child = NULL;
|
||||
if (*value != '{')
|
||||
{
|
||||
/* not an object! */
|
||||
@@ -1264,7 +1264,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(hooks)))
|
||||
{
|
||||
/* memory fail */
|
||||
@@ -1315,12 +1315,12 @@ static char *print_object(const cJSON *item, size_t depth, int fmt, printbuffer
|
||||
char **entries = NULL;
|
||||
char **names = NULL;
|
||||
char *out = NULL;
|
||||
char *ptr;
|
||||
char *ret;
|
||||
char *str;
|
||||
char *ptr = NULL;
|
||||
char *ret = NULL;
|
||||
char *str = NULL;
|
||||
size_t len = 7;
|
||||
size_t i = 0;
|
||||
size_t j;
|
||||
size_t j = 0;
|
||||
cJSON *child = item->child;
|
||||
size_t numentries = 0;
|
||||
int fail = 0;
|
||||
@@ -2001,7 +2001,7 @@ cJSON *cJSON_CreateObject(void)
|
||||
/* Create Arrays: */
|
||||
cJSON *internal_cJSON_CreateIntArray(const int *numbers, size_t count, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
size_t i;
|
||||
size_t i = 0;
|
||||
cJSON *n = NULL;
|
||||
cJSON *p = NULL;
|
||||
cJSON *a = internal_cJSON_CreateArray(hooks);
|
||||
@@ -2033,7 +2033,7 @@ cJSON *cJSON_CreateIntArray(const int *numbers, size_t count)
|
||||
|
||||
cJSON *internal_cJSON_CreateFloatArray(const float *numbers, size_t count, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
size_t i;
|
||||
size_t i = 0;
|
||||
cJSON *n = NULL;
|
||||
cJSON *p = NULL;
|
||||
cJSON *a = internal_cJSON_CreateArray(hooks);
|
||||
@@ -2065,7 +2065,7 @@ cJSON *cJSON_CreateFloatArray(const float *numbers, size_t count)
|
||||
|
||||
cJSON *internal_cJSON_CreateDoubleArray(const double *numbers, size_t count, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
size_t i;
|
||||
size_t i = 0;
|
||||
cJSON *n = NULL;
|
||||
cJSON *p = NULL;
|
||||
cJSON *a = internal_cJSON_CreateArray(hooks);
|
||||
@@ -2097,7 +2097,7 @@ cJSON *cJSON_CreateDoubleArray(const double *numbers, size_t count)
|
||||
|
||||
cJSON *internal_cJSON_CreateStringArray(const char **strings, size_t count, const cJSON_Hooks * const hooks)
|
||||
{
|
||||
size_t i;
|
||||
size_t i = 0;
|
||||
cJSON *n = NULL;
|
||||
cJSON *p = NULL;
|
||||
cJSON *a = internal_cJSON_CreateArray(hooks);
|
||||
|
||||
@@ -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 != cJSON_Array)
|
||||
{
|
||||
/* malformed patches. */
|
||||
@@ -550,7 +550,7 @@ static void cJSONUtils_CompareToPatch(cJSON *patches, const char *path, cJSON *f
|
||||
|
||||
case cJSON_Array:
|
||||
{
|
||||
size_t c;
|
||||
size_t 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);
|
||||
|
||||
|
||||
24
test.c
24
test.c
@@ -28,8 +28,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)
|
||||
@@ -48,9 +48,9 @@ void doit(char *text)
|
||||
/* Read a file, parse, render back, etc. */
|
||||
void dofile(char *filename)
|
||||
{
|
||||
FILE *f;
|
||||
size_t len;
|
||||
char *data;
|
||||
FILE *f = NULL;
|
||||
size_t len = 0;
|
||||
char *data = NULL;
|
||||
|
||||
/* open in read binary mode */
|
||||
f = fopen(filename,"rb");
|
||||
@@ -86,13 +86,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] =
|
||||
|
||||
@@ -10,9 +10,9 @@ int main(void)
|
||||
char *patchtext = NULL;
|
||||
char *patchedtext = NULL;
|
||||
|
||||
size_t i;
|
||||
size_t 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;
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user