fix: print_number didn't abort when out of memory

This commit is contained in:
Max Bruckner 2017-03-15 00:09:45 +01:00
parent 030d0c14cc
commit cf1842dc6f

44
cJSON.c
View File

@ -372,28 +372,30 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
/* This is a nice tradeoff. */ /* This is a nice tradeoff. */
output_pointer = ensure(output_buffer, 64, hooks); output_pointer = ensure(output_buffer, 64, hooks);
if (output_pointer != NULL) if (output_pointer == NULL)
{ {
/* This checks for NaN and Infinity */ return false;
if ((d * 0) != 0) }
{
length = sprintf((char*)output_pointer, "null"); /* This checks for NaN and Infinity */
} if ((d * 0) != 0)
else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60)) {
{ length = sprintf((char*)output_pointer, "null");
/* integer */ }
length = sprintf((char*)output_pointer, "%.0f", d); else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60))
trim_zeroes = false; /* don't remove zeroes for "big integers" */ {
} /* integer */
else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9)) length = sprintf((char*)output_pointer, "%.0f", d);
{ trim_zeroes = false; /* don't remove zeroes for "big integers" */
length = sprintf((char*)output_pointer, "%e", d); }
trim_zeroes = false; /* don't remove zeroes in engineering notation */ else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9))
} {
else length = sprintf((char*)output_pointer, "%e", d);
{ trim_zeroes = false; /* don't remove zeroes in engineering notation */
length = sprintf((char*)output_pointer, "%f", d); }
} else
{
length = sprintf((char*)output_pointer, "%f", d);
} }
/* sprintf failed */ /* sprintf failed */