Fix null pointer crash, closes #536 (#538)

This commit is contained in:
Alan Wang 2020-12-17 15:42:31 +08:00 committed by GitHub
parent 4100379a04
commit 7b6645794d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

36
cJSON.c
View File

@ -2548,12 +2548,8 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
} }
a = cJSON_CreateArray(); a = cJSON_CreateArray();
if (!a)
{
return NULL;
}
for(i = 0; i < (size_t)count; i++) for(i = 0; a && (i < (size_t)count); i++)
{ {
n = cJSON_CreateNumber(numbers[i]); n = cJSON_CreateNumber(numbers[i]);
if (!n) if (!n)
@ -2571,7 +2567,10 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
} }
p = n; p = n;
} }
if (a && a->child) {
a->child->prev = n; a->child->prev = n;
}
return a; return a;
} }
@ -2589,12 +2588,8 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
} }
a = cJSON_CreateArray(); a = cJSON_CreateArray();
if (!a)
{
return NULL;
}
for(i = 0; i < (size_t)count; i++) for(i = 0; a && (i < (size_t)count); i++)
{ {
n = cJSON_CreateNumber((double)numbers[i]); n = cJSON_CreateNumber((double)numbers[i]);
if(!n) if(!n)
@ -2612,7 +2607,10 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
} }
p = n; p = n;
} }
if (a && a->child) {
a->child->prev = n; a->child->prev = n;
}
return a; return a;
} }
@ -2630,12 +2628,8 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
} }
a = cJSON_CreateArray(); a = cJSON_CreateArray();
if (!a)
{
return NULL;
}
for(i = 0; i < (size_t)count; i++) for(i = 0; a && (i < (size_t)count); i++)
{ {
n = cJSON_CreateNumber(numbers[i]); n = cJSON_CreateNumber(numbers[i]);
if(!n) if(!n)
@ -2653,7 +2647,10 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
} }
p = n; p = n;
} }
if (a && a->child) {
a->child->prev = n; a->child->prev = n;
}
return a; return a;
} }
@ -2671,12 +2668,8 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
} }
a = cJSON_CreateArray(); a = cJSON_CreateArray();
if (!a)
{
return NULL;
}
for (i = 0; i < (size_t)count; i++) for (i = 0; a && (i < (size_t)count); i++)
{ {
n = cJSON_CreateString(strings[i]); n = cJSON_CreateString(strings[i]);
if(!n) if(!n)
@ -2694,7 +2687,10 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
} }
p = n; p = n;
} }
if (a && a->child) {
a->child->prev = n; a->child->prev = n;
}
return a; return a;
} }