From e543afeaa371c8d47b928dfd564093ca12810c55 Mon Sep 17 00:00:00 2001 From: youbing54 Date: Mon, 12 May 2025 16:11:27 +0800 Subject: [PATCH] =?UTF-8?q?IssueNo:=20https://gitee.com/openharmony/third?= =?UTF-8?q?=5Fparty=5FcJSON/issues/IC750Q=20describe:=20CVE-2023-26819=20?= =?UTF-8?q?=E6=BC=8F=E6=B4=9E=E4=BF=AE=E5=A4=8D=20Feature=20or=20Bugfix:?= =?UTF-8?q?=20Bugfix=20Binary=20Source:Yes=20Signed-off-by:=20youbing54?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cJSON.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index ae0783a..cf9907e 100644 --- a/cJSON.c +++ b/cJSON.c @@ -424,9 +424,11 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu { double number = 0; unsigned char *after_end = NULL; - unsigned char number_c_string[64]; + unsigned char *number_c_string; unsigned char decimal_point = get_decimal_point(); size_t i = 0; + size_t number_string_length = 0; + cJSON_bool has_decimal_point = false; if ((input_buffer == NULL) || (input_buffer->content == NULL)) { @@ -455,10 +457,13 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu case 'e': case 'E': number_c_string[i] = buffer_at_offset(input_buffer)[i]; + number_string_length++; break; case '.': number_c_string[i] = decimal_point; + number_string_length++; + has_decimal_point = true; break; default: @@ -466,11 +471,34 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu } } loop_end: + /* malloc for temporary buffer, add 1 for '\0' */ + number_c_string = (unsigned char *) input_buffer->hooks.allocate(number_string_length + 1); + if (number_c_string == NULL) + { + return false; /* allocation failure */ + } + + memcpy(number_c_string, buffer_at_offset(input_buffer), number_string_length); + number_c_string[number_string_length] = '\0'; + + if (has_decimal_point) + { + for (i = 0; i < number_string_length; i++) + { + if (number_c_string[i] == '.') + { + /* replace '.' with the decimal point of the current locale (for strtod) */ + number_c_string[i] = decimal_point; + } + } + } number_c_string[i] = '\0'; number = strtod((const char*)number_c_string, (char**)&after_end); if (number_c_string == after_end) { + /* free the temporary buffer */ + input_buffer->hooks.deallocate(number_c_string); return false; /* parse_error */ } @@ -493,6 +521,8 @@ loop_end: item->type = cJSON_Number; input_buffer->offset += (size_t)(after_end - number_c_string); + /* free the temporary buffer */ + input_buffer->hooks.deallocate(number_c_string); return true; } #endif /* __CJSON_USE_INT64 */ -- Gitee