json: Print "nan", "-inf", "inf" if needed.

* src/cJSON.c (print_number): Print NaN and INF.
--

GnuPG-bug-id: 4328
Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2019-06-04 10:56:48 +02:00
parent fabe96126b
commit f56c996318
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -216,6 +216,8 @@ print_number (cJSON * item)
{ {
char *str; char *str;
double d = item->valuedouble; double d = item->valuedouble;
int i;
if (fabs (((double) item->valueint) - d) <= DBL_EPSILON && d <= INT_MAX if (fabs (((double) item->valueint) - d) <= DBL_EPSILON && d <= INT_MAX
&& d >= INT_MIN) && d >= INT_MIN)
{ {
@ -229,7 +231,11 @@ print_number (cJSON * item)
str = xtrymalloc (64); /* This is a nice tradeoff. */ str = xtrymalloc (64); /* This is a nice tradeoff. */
if (str) if (str)
{ {
if (fabs (floor (d) - d) <= DBL_EPSILON && fabs (d) < 1.0e60) if (isnan (d))
strcpy (str, "nan");
else if ((i = isinf (d)))
strcpy (str, i > 0? "inf" : ":-inf");
else if (fabs (floor (d) - d) <= DBL_EPSILON && fabs (d) < 1.0e60)
sprintf (str, "%.0f", d); sprintf (str, "%.0f", d);
else if (fabs (d) < 1.0e-6 || fabs (d) > 1.0e9) else if (fabs (d) < 1.0e-6 || fabs (d) > 1.0e9)
sprintf (str, "%e", d); sprintf (str, "%e", d);