From 990f990247bc12231f9acff495eecaa67ee4ed4e Mon Sep 17 00:00:00 2001 From: Yikun Wei Date: Mon, 9 Nov 2026 16:46:25 +0800 Subject: [PATCH] Fix: avoid strict-aliasing violation in parse_number In parse_number, strtod's end pointer was obtained by casting (unsigned char **) to (char **) and writing through it, which is a strict-aliasing violation (undefined behavior). Use an intermediate char* variable and assign it back to after_end instead. No behavior change; verified cJSON.c still compiles and the existing parse_number tests pass. --- cJSON.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index 88c2d95b3..0c9684451 100644 --- a/cJSON.c +++ b/cJSON.c @@ -375,7 +375,12 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu } } - number = strtod((const char*)number_c_string, (char**)&after_end); + /* Use a char* temporary for strtod's end pointer. Passing + * (char**)&after_end would violate strict aliasing rules (UB), + * because after_end is an unsigned char*. */ + char *strtod_end = NULL; + number = strtod((const char*)number_c_string, &strtod_end); + after_end = (unsigned char*)strtod_end; if (number_c_string == after_end) { /* free the temporary buffer */