Fix float comparison also in cJSON_Utils

This commit is contained in:
No Default Name 2022-05-01 13:33:21 +02:00
parent e79376f2d9
commit 4c2b3d8949
2 changed files with 16 additions and 9 deletions

View File

@ -582,15 +582,15 @@ static void update_offset(printbuffer * const buffer)
/* securely comparison of floating-point variables */
#ifdef CJSON_FLOAT_USE_FLOAT
static cJSON_bool compare_cJSON_float(float a, float b)
static cJSON_bool compare_cJSON_float(cJSON_float a, cJSON_float b)
{
float maxVal = fabsf(a) > fabsf(b) ? fabsf(a) : fabsf(b);
cJSON_float maxVal = fabsf(a) > fabsf(b) ? fabsf(a) : fabsf(b);
return (fabsf(a - b) <= maxVal * FLT_EPSILON);
}
#else
static cJSON_bool compare_cJSON_float(double a, double b)
static cJSON_bool compare_cJSON_float(cJSON_float a, cJSON_float b)
{
double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
cJSON_float maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
return (fabs(a - b) <= maxVal * DBL_EPSILON);
}
#endif

View File

@ -109,12 +109,19 @@ static int compare_strings(const unsigned char *string1, const unsigned char *st
}
/* securely comparison of floating-point variables */
static cJSON_bool compare_double(double a, double b)
#ifdef CJSON_FLOAT_USE_FLOAT
static cJSON_bool compare_cJSON_float(cJSON_float a, cJSON_float b)
{
double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
cJSON_float maxVal = fabsf(a) > fabsf(b) ? fabsf(a) : fabsf(b);
return (fabsf(a - b) <= maxVal * FLT_EPSILON);
}
#else
static cJSON_bool compare_cJSON_float(cJSON_float a, cJSON_float b)
{
cJSON_float maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
return (fabs(a - b) <= maxVal * DBL_EPSILON);
}
#endif
/* 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)
@ -612,7 +619,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) || (!compare_double(a->valuedouble, b->valuedouble)))
if ((a->valueint != b->valueint) || (!compare_cJSON_float(a->valuedouble, b->valuedouble)))
{
return false;
}
@ -1154,7 +1161,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) || !compare_double(from->valuedouble, to->valuedouble))
if ((from->valueint != to->valueint) || !compare_cJSON_float(from->valuedouble, to->valuedouble))
{
compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
}