fix bug 2895595 from jshvrsn whereby values outside of INT_MIN/INT_MAX range are printed incorrectly as ints.

git-svn-id: http://svn.code.sf.net/p/cjson/code@17 e3330c51-1366-4df0-8b21-3ccf24e3d50e
This commit is contained in:
Dave Gamble 2009-11-11 00:15:53 +00:00
parent 69dca155a9
commit 0dbe29ffe8

View File

@ -28,6 +28,7 @@
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include "cJSON.h"
#if defined(WINDOWS) || defined(__WIN32__) || defined(WIN32) || defined(_WIN32)
@ -112,7 +113,7 @@ static char *print_number(cJSON *item)
{
char *str;
double d=item->valuedouble;
if (fabs(((double)item->valueint)-d)<=DBL_EPSILON)
if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
{
str=(char*)cJSON_malloc(21); // 2^64+1 can be represented in 21 chars.
sprintf(str,"%d",item->valueint);