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 heap buffer overflow #852

Merged
merged 2 commits into from
May 6, 2024
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
5 changes: 5 additions & 0 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,11 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu
current_item = new_item;
}

if (cannot_access_at_index(input_buffer, 1))
{
goto fail; /* nothing comes after the comma */
}

/* parse the name of the child */
input_buffer->offset++;
buffer_skip_whitespace(input_buffer);
Expand Down
28 changes: 28 additions & 0 deletions tests/parse_examples.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,33 @@ static void test14_should_not_be_parsed(void)
}
}

/* Address Sanitizer */
static void test15_should_not_heap_buffer_overflow(void)
{
const char *strings[] = {
"{\"1\":1,",
"{\"1\":1, ",
};

size_t i;

for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i+=1)
{
const char *json_string = strings[i];
size_t len = strlen(json_string);
cJSON *json = NULL;

char *exact_size_heap = (char*)malloc(len);
TEST_ASSERT_NOT_NULL(exact_size_heap);

memcpy(exact_size_heap, json_string, len);
json = cJSON_ParseWithLength(exact_size_heap, len);

cJSON_Delete(json);
free(exact_size_heap);
}
}

int CJSON_CDECL main(void)
{
UNITY_BEGIN();
Expand All @@ -267,5 +294,6 @@ int CJSON_CDECL main(void)
RUN_TEST(test12_should_not_be_parsed);
RUN_TEST(test13_should_be_parsed_without_null_termination);
RUN_TEST(test14_should_not_be_parsed);
RUN_TEST(test15_should_not_heap_buffer_overflow);
return UNITY_END();
}
Loading