Optimise while loop checks in parse_string

This gave an approximate 4% improvement in tests.
This commit is contained in:
Roger Light 2023-08-10 10:11:18 +01:00
parent e64b984ddc
commit 9735349087

View File

@ -787,18 +787,23 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu
{ {
/* calculate approximate size of the output (overestimate) */ /* calculate approximate size of the output (overestimate) */
while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
size_t remaining_length = input_buffer->length;
while (remaining_length > 0 && (*input_end != '\"'))
{ {
remaining_length--;
/* is escape sequence */ /* is escape sequence */
if (input_end[0] == '\\') if (input_end[0] == '\\')
{ {
if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) if (remaining_length == 0)
{ {
/* prevent buffer overflow when last input character is a backslash */ /* prevent buffer overflow when last input character is a backslash */
goto fail; goto fail;
} }
skipped_bytes++; skipped_bytes++;
input_end++; input_end++;
remaining_length--;
} }
input_end++; input_end++;
} }