cJSON_SetNumberValue: Fix undefined double to int conversion

This might cause slight changes in behavior, but it shouldn't break
anything that is not already broken (for example the original macro
could either return a double, or an integer or whatever depending on if
object is NULL or not.)
This commit is contained in:
Max Bruckner 2017-02-07 00:05:27 +01:00
parent dded751757
commit 87f77274de
2 changed files with 23 additions and 2 deletions

19
cJSON.c
View File

@ -194,6 +194,25 @@ static const unsigned char *parse_number(cJSON *item, const unsigned char *num)
return endpointer;
}
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
double cJSON_SetNumberHelper(cJSON *object, double number)
{
if (number >= INT_MAX)
{
object->valueint = INT_MAX;
}
else if (number <= INT_MIN)
{
object->valueint = INT_MIN;
}
else
{
object->valueint = cJSON_Number;
}
return object->valuedouble = number;
}
/* calculate the next largest power of 2 */
static int pow2gt (int x)
{

View File

@ -169,8 +169,10 @@ extern void cJSON_Minify(char *json);
#define cJSON_AddRawToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateRaw(s))
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object,val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))
#define cJSON_SetNumberValue(object,val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* helper for the cJSON_SetNumberValue macro */
extern double cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object) ? cJSON_SetNumberHelper(object, (double)number) : (number))
/* Macro for iterating over an array */
#define cJSON_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next)