From bb0b618e7150a933fbce0cc81b73d6e8ed006179 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 20 Apr 2014 19:21:55 +0200 Subject: [PATCH] Make print_number work for non-finite numbers again. Refs #4865 --- third-party/cJSON/cJSON.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/third-party/cJSON/cJSON.c b/third-party/cJSON/cJSON.c index 70ed0014b..f1bef65ff 100644 --- a/third-party/cJSON/cJSON.c +++ b/third-party/cJSON/cJSON.c @@ -127,12 +127,20 @@ static char *print_number(cJSON *item) } else { - str=(char*)cJSON_malloc(64 + (int)log10(fabs(d))); /* This is a nice tradeoff. */ - if (str) + if (d != d) { - if (d != d) strcpy(str, "0"); - else if (fabs(floor(d)-d)<=DBL_EPSILON) sprintf(str,"%.0f",d); - else sprintf(str,"%.*e",(int)log10(d) + 6,d); + str=(char*)cJSON_malloc(2); + if (str) + strcpy(str, "0"); + } + else + { + str = (char*)cJSON_malloc(64 + (int)log10(fabs(d))); /* This is a nice tradeoff. */ + if (str) + { + if (fabs(floor(d) - d) <= DBL_EPSILON) sprintf(str, "%.0f", d); + else sprintf(str, "%.*e", (int)log10(d) + 6, d); + } } } return str;