Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix several null pointer problems on allocation failure #526

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -2548,7 +2548,12 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
}

a = cJSON_CreateArray();
for(i = 0; a && (i < (size_t)count); i++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when allocation failure, cJSON_CreateArray() will return NULL, then it won't enter the for loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it won't enter the for loop, but 'a->child' will crush.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the line after for loop, a->child->prev = n;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I didn't notice that.

if (!a)
{
return NULL;
}

for(i = 0; i < (size_t)count; i++)
{
n = cJSON_CreateNumber(numbers[i]);
if (!n)
Expand Down Expand Up @@ -2584,8 +2589,12 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
}

a = cJSON_CreateArray();
if (!a)
{
return NULL;
}

for(i = 0; a && (i < (size_t)count); i++)
for(i = 0; i < (size_t)count; i++)
{
n = cJSON_CreateNumber((double)numbers[i]);
if(!n)
Expand Down Expand Up @@ -2621,8 +2630,12 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
}

a = cJSON_CreateArray();
if (!a)
{
return NULL;
}

for(i = 0;a && (i < (size_t)count); i++)
for(i = 0; i < (size_t)count; i++)
{
n = cJSON_CreateNumber(numbers[i]);
if(!n)
Expand Down Expand Up @@ -2658,8 +2671,12 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
}

a = cJSON_CreateArray();
if (!a)
{
return NULL;
}

for (i = 0; a && (i < (size_t)count); i++)
for (i = 0; i < (size_t)count; i++)
{
n = cJSON_CreateString(strings[i]);
if(!n)
Expand Down
49 changes: 49 additions & 0 deletions tests/cjson_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,50 @@ static void cjson_add_true_should_fail_on_allocation_failure(void)
cJSON_Delete(root);
}

static void cjson_create_int_array_should_fail_on_allocation_failure(void)
{
int numbers[] = {1, 2, 3};

cJSON_InitHooks(&failing_hooks);

TEST_ASSERT_NULL(cJSON_CreateIntArray(numbers, 3));

cJSON_InitHooks(NULL);
}

static void cjson_create_float_array_should_fail_on_allocation_failure(void)
{
float numbers[] = {1.0f, 2.0f, 3.0f};

cJSON_InitHooks(&failing_hooks);

TEST_ASSERT_NULL(cJSON_CreateFloatArray(numbers, 3));

cJSON_InitHooks(NULL);
}

static void cjson_create_double_array_should_fail_on_allocation_failure(void)
{
double numbers[] = {1.0, 2.0, 3.0};

cJSON_InitHooks(&failing_hooks);

TEST_ASSERT_NULL(cJSON_CreateDoubleArray(numbers, 3));

cJSON_InitHooks(NULL);
}

static void cjson_create_string_array_should_fail_on_allocation_failure(void)
{
const char* strings[] = {"1", "2", "3"};

cJSON_InitHooks(&failing_hooks);

TEST_ASSERT_NULL(cJSON_CreateStringArray(strings, 3));

cJSON_InitHooks(NULL);
}

static void cjson_add_false_should_add_false(void)
{
cJSON *root = cJSON_CreateObject();
Expand Down Expand Up @@ -390,6 +434,11 @@ int CJSON_CDECL main(void)
RUN_TEST(cjson_add_true_should_fail_with_null_pointers);
RUN_TEST(cjson_add_true_should_fail_on_allocation_failure);

RUN_TEST(cjson_create_int_array_should_fail_on_allocation_failure);
RUN_TEST(cjson_create_float_array_should_fail_on_allocation_failure);
RUN_TEST(cjson_create_double_array_should_fail_on_allocation_failure);
RUN_TEST(cjson_create_string_array_should_fail_on_allocation_failure);

RUN_TEST(cjson_add_false_should_add_false);
RUN_TEST(cjson_add_false_should_fail_with_null_pointers);
RUN_TEST(cjson_add_false_should_fail_on_allocation_failure);
Expand Down