mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Merge 3cd7b59a0c into cb8693b058
This commit is contained in:
15
cJSON.c
15
cJSON.c
@@ -535,6 +535,11 @@ static void update_offset(printbuffer * const buffer)
|
||||
}
|
||||
|
||||
/* securely comparison of floating-point variables */
|
||||
static cJSON_bool compare_float(float a, float b)
|
||||
{
|
||||
float maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
|
||||
return (fabs(a - b) <= maxVal * FLT_EPSILON);
|
||||
}
|
||||
static cJSON_bool compare_double(double a, double b)
|
||||
{
|
||||
double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
|
||||
@@ -550,6 +555,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
||||
size_t i = 0;
|
||||
unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */
|
||||
unsigned char decimal_point = get_decimal_point();
|
||||
float test_f = 0.0;
|
||||
double test = 0.0;
|
||||
|
||||
if (output_buffer == NULL)
|
||||
@@ -568,7 +574,13 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
|
||||
/* Try 7 decimal places of precision to avoid nonsignificant nonzero digits */
|
||||
length = sprintf((char*)number_buffer, "%1.7g", (float)d);
|
||||
|
||||
/* Check whether the original double can be recovered */
|
||||
if ((sscanf((char*)number_buffer, "%g", &test_f) != 1) || !compare_float(test_f, (float)d))
|
||||
{
|
||||
/* If not, print with 17 decimal places of precision */
|
||||
length = sprintf((char*)number_buffer, "%1.15g", d);
|
||||
|
||||
/* Check whether the original double can be recovered */
|
||||
@@ -578,6 +590,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)))
|
||||
|
||||
Reference in New Issue
Block a user