cJSON

mirror of Dave's cJSON
git clone git://git.thc420.xyz/cJSON
Log | Files | Refs | README | LICENSE

commit 11b8c97f417737f4c7b03bab6a65765f30799b1d
parent affedd65ba768fca9d8e8c6c08bf5904fd33394e
Author: Kevin Branigan <kbranigan@gmail.com>
Date:   Thu,  1 Oct 2015 11:36:19 -0400

Merge pull request #17 from Scypho/master

Handle invalid numbers

Diffstat:
McJSON.c | 7++++---
Mtest.c | 3+++
2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/cJSON.c b/cJSON.c @@ -166,9 +166,10 @@ static char *print_number(cJSON *item,printbuffer *p) else str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */ if (str) { - if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d); - else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d); - else sprintf(str,"%f",d); + if (fpclassify(d) != FP_ZERO && !isnormal(d)) sprintf(str,"null"); + else if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60) sprintf(str,"%.0f",d); + else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d); + else sprintf(str,"%f",d); } } return str; diff --git a/test.c b/test.c @@ -131,6 +131,9 @@ void create_objects() out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); + root=cJSON_CreateObject(); + cJSON_AddNumberToObject(root,"number", 1.0/0.0); + out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); } int main (int argc, const char * argv[]) {