Skip to content

Commit

Permalink
fix: print_number didn't abort when out of memory
Browse files Browse the repository at this point in the history
  • Loading branch information
FSMaxB committed Mar 14, 2017
1 parent 030d0c1 commit cf1842d
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,28 +372,30 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out

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

/* This checks for NaN and Infinity */
if ((d * 0) != 0)
{
length = sprintf((char*)output_pointer, "null");
}
else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60))
{
/* integer */
length = sprintf((char*)output_pointer, "%.0f", d);
trim_zeroes = false; /* don't remove zeroes for "big integers" */
}
else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9))
{
length = sprintf((char*)output_pointer, "%e", d);
trim_zeroes = false; /* don't remove zeroes in engineering notation */
}
else
{
length = sprintf((char*)output_pointer, "%f", d);
}

/* sprintf failed */
Expand Down

0 comments on commit cf1842d

Please sign in to comment.