Skip to content

Make cJSON_ParseWithLength and cJSON_Parse consistent #941

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 5 additions & 17 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
unsigned char *output = NULL;

/* not a string */
if (buffer_at_offset(input_buffer)[0] != '\"')
if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != '\"')
{
goto fail;
}
Expand Down Expand Up @@ -1099,11 +1099,6 @@ static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
buffer->offset++;
}

if (buffer->offset == buffer->length)
{
buffer->offset--;
}

return buffer;
}

Expand All @@ -1125,17 +1120,12 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)

CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
{
size_t buffer_length;

if (NULL == value)
{
return NULL;
}

/* Adding null character size due to require_null_terminated. */
buffer_length = strlen(value) + sizeof("");

return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated);
return cJSON_ParseWithLengthOpts(value, (size_t)strlen(value), return_parse_end, require_null_terminated);
}

/* Parse an object - create a new root, and populate. */
Expand Down Expand Up @@ -1174,7 +1164,7 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer
if (require_null_terminated)
{
buffer_skip_whitespace(&buffer);
if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
if ((buffer.offset > buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
{
goto fail;
}
Expand Down Expand Up @@ -1204,7 +1194,7 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer
}
else if (buffer.length > 0)
{
local_error.position = buffer.length - 1;
local_error.position = buffer.length;
}

if (return_parse_end != NULL)
Expand Down Expand Up @@ -1500,7 +1490,7 @@ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buf
}
input_buffer->depth++;

if (buffer_at_offset(input_buffer)[0] != '[')
if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != '[')
{
/* not an array */
goto fail;
Expand All @@ -1517,7 +1507,6 @@ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buf
/* check if we skipped to the end of the buffer */
if (cannot_access_at_index(input_buffer, 0))
{
input_buffer->offset--;
goto fail;
}

Expand Down Expand Up @@ -1675,7 +1664,6 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu
/* check if we skipped to the end of the buffer */
if (cannot_access_at_index(input_buffer, 0))
{
input_buffer->offset--;
goto fail;
}

Expand Down