|
@@ -480,6 +480,12 @@ static void update_offset(printbuffer * const buffer)
|
|
buffer->offset += strlen((const char*)buffer_pointer);
|
|
buffer->offset += strlen((const char*)buffer_pointer);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/* securely comparison of floating-point variables */
|
|
|
|
+static cJSON_bool compare_double(double a, double b)
|
|
|
|
+{
|
|
|
|
+ return (fabs(a - b) <= a * CJSON_DOUBLE_PRECIION);
|
|
|
|
+}
|
|
|
|
+
|
|
/* Render the number nicely from the given item into a string. */
|
|
/* Render the number nicely from the given item into a string. */
|
|
static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer)
|
|
static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer)
|
|
{
|
|
{
|
|
@@ -497,7 +503,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
|
}
|
|
}
|
|
|
|
|
|
/* This checks for NaN and Infinity */
|
|
/* This checks for NaN and Infinity */
|
|
- if ((d * 0) != 0)
|
|
|
|
|
|
+ if (!compare_double(d * 0, 0))
|
|
{
|
|
{
|
|
length = sprintf((char*)number_buffer, "null");
|
|
length = sprintf((char*)number_buffer, "null");
|
|
}
|
|
}
|
|
@@ -507,7 +513,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
|
|
length = sprintf((char*)number_buffer, "%1.15g", d);
|
|
length = sprintf((char*)number_buffer, "%1.15g", d);
|
|
|
|
|
|
/* Check whether the original double can be recovered */
|
|
/* Check whether the original double can be recovered */
|
|
- if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((double)test != d))
|
|
|
|
|
|
+ if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
|
|
{
|
|
{
|
|
/* If not, print with 17 decimal places of precision */
|
|
/* If not, print with 17 decimal places of precision */
|
|
length = sprintf((char*)number_buffer, "%1.17g", d);
|
|
length = sprintf((char*)number_buffer, "%1.17g", d);
|
|
@@ -2876,7 +2882,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons
|
|
return true;
|
|
return true;
|
|
|
|
|
|
case cJSON_Number:
|
|
case cJSON_Number:
|
|
- if (a->valuedouble == b->valuedouble)
|
|
|
|
|
|
+ if (compare_double(a->valuedouble, b->valuedouble))
|
|
{
|
|
{
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|