Fix clang -Wfloat-equal warning
This commit is contained in:
@@ -39,6 +39,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning (pop)
|
||||
@@ -105,6 +106,13 @@ static int compare_strings(const unsigned char *string1, const unsigned char *st
|
||||
return tolower(*string1) - tolower(*string2);
|
||||
}
|
||||
|
||||
/* securely comparison of floating-point variables */
|
||||
static cJSON_bool compare_double(double a, double b)
|
||||
{
|
||||
return (fabs(a - b) <= a * CJSON_DOUBLE_PRECIION);
|
||||
}
|
||||
|
||||
|
||||
/* Compare the next path element of two JSON pointers, two NULL pointers are considered unequal: */
|
||||
static cJSON_bool compare_pointers(const unsigned char *name, const unsigned char *pointer, const cJSON_bool case_sensitive)
|
||||
{
|
||||
@@ -595,7 +603,7 @@ static cJSON_bool compare_json(cJSON *a, cJSON *b, const cJSON_bool case_sensiti
|
||||
{
|
||||
case cJSON_Number:
|
||||
/* numeric mismatch. */
|
||||
if ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble))
|
||||
if ((a->valueint != b->valueint) || (!compare_double(a->valuedouble, b->valuedouble)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -1135,7 +1143,7 @@ static void create_patches(cJSON * const patches, const unsigned char * const pa
|
||||
switch (from->type & 0xFF)
|
||||
{
|
||||
case cJSON_Number:
|
||||
if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
|
||||
if ((from->valueint != to->valueint) || (compare_double(from->valuedouble, to->valuedouble)))
|
||||
{
|
||||
compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user