fix:print_number() print incorrect integer

This commit is contained in:
“uinontech_yuxiaojun” 2022-01-21 09:47:56 +08:00
parent 203a0dec6f
commit b64963332f

View File

@ -563,6 +563,12 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
length = sprintf((char*)number_buffer, "null");
}
else
{
if(item->valuedouble == item->valueint)
{
length = sprintf((char*)number_buffer, "%d", item->valueint);
}
else
{
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
length = sprintf((char*)number_buffer, "%1.15g", d);
@ -574,6 +580,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
length = sprintf((char*)number_buffer, "%1.17g", d);
}
}
}
/* sprintf failed or buffer overrun occurred */
if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))