diff --git a/Makefile b/Makefile index 1c07922e01f51ee6057a30077fe8531bf60cfcdc_TWFrZWZpbGU=..8b121b387ef919fd917acd43613e811ed032559a_TWFrZWZpbGU= 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ endif PIC_FLAGS = -fPIC -R_CFLAGS = $(PIC_FLAGS) -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wc++-compat -Wundef -Wswitch-default -Wconversion -Wfloat-equal $(CFLAGS) +R_CFLAGS = $(PIC_FLAGS) -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wc++-compat -Wundef -Wswitch-default -Wconversion $(CFLAGS) uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') diff --git a/cJSON.c b/cJSON.c index 1c07922e01f51ee6057a30077fe8531bf60cfcdc_Y0pTT04uYw==..8b121b387ef919fd917acd43613e811ed032559a_Y0pTT04uYw== 100644 --- a/cJSON.c +++ b/cJSON.c @@ -43,6 +43,7 @@ #include <stdlib.h> #include <limits.h> #include <ctype.h> +#include <float.h> #ifdef ENABLE_LOCALES #include <locale.h> @@ -68,6 +69,14 @@ #endif #define false ((cJSON_bool)0) +/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ +#ifndef isinf +#define isinf(d) (isnan((d - d)) && !isnan(d)) +#endif +#ifndef isnan +#define isnan(d) (d != d) +#endif + typedef struct { const unsigned char *json; size_t position; @@ -483,7 +492,8 @@ /* securely comparison of floating-point variables */ static cJSON_bool compare_double(double a, double b) { - return (fabs(a - b) <= CJSON_DOUBLE_PRECISION); + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); } /* Render the number nicely from the given item into a string. */ @@ -503,7 +513,7 @@ } /* This checks for NaN and Infinity */ - if ((d * 0) != 0) + if (isnan(d) || isinf(d)) { length = sprintf((char*)number_buffer, "null"); } diff --git a/cJSON.h b/cJSON.h index 1c07922e01f51ee6057a30077fe8531bf60cfcdc_Y0pTT04uaA==..8b121b387ef919fd917acd43613e811ed032559a_Y0pTT04uaA== 100644 --- a/cJSON.h +++ b/cJSON.h @@ -137,11 +137,6 @@ #define CJSON_NESTING_LIMIT 1000 #endif -/* Precision of double variables comparison */ -#ifndef CJSON_DOUBLE_PRECISION -#define CJSON_DOUBLE_PRECISION .0000000000000001 -#endif - /* returns the version of cJSON as a string */ CJSON_PUBLIC(const char*) cJSON_Version(void); diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 1c07922e01f51ee6057a30077fe8531bf60cfcdc_Y0pTT05fVXRpbHMuYw==..8b121b387ef919fd917acd43613e811ed032559a_Y0pTT05fVXRpbHMuYw== 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -40,6 +40,8 @@ #include <stdio.h> #include <limits.h> #include <math.h> +#include <float.h> +#include <math.h> #if defined(_MSC_VER) #pragma warning (pop) @@ -109,7 +111,8 @@ /* securely comparison of floating-point variables */ static cJSON_bool compare_double(double a, double b) { - return (fabs(a - b) <= CJSON_DOUBLE_PRECISION); + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); }