提交 6f271e51 编写于 作者: M Max Bruckner

print_number: Use sprintf's return value

This is used to update the buffer offset and determine success
上级 bee069b4
...@@ -329,6 +329,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const ...@@ -329,6 +329,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
{ {
unsigned char *output_pointer = NULL; unsigned char *output_pointer = NULL;
double d = item->valuedouble; double d = item->valuedouble;
int length = 0;
if (output_buffer == NULL) if (output_buffer == NULL)
{ {
...@@ -342,7 +343,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const ...@@ -342,7 +343,7 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
output_pointer = ensure(output_buffer, 21, hooks); output_pointer = ensure(output_buffer, 21, hooks);
if (output_pointer != NULL) if (output_pointer != NULL)
{ {
sprintf((char*)output_pointer, "%d", item->valueint); length = sprintf((char*)output_pointer, "%d", item->valueint);
} }
} }
/* value is a floating point number */ /* value is a floating point number */
...@@ -355,23 +356,31 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const ...@@ -355,23 +356,31 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
/* This checks for NaN and Infinity */ /* This checks for NaN and Infinity */
if ((d * 0) != 0) if ((d * 0) != 0)
{ {
sprintf((char*)output_pointer, "null"); length = sprintf((char*)output_pointer, "null");
} }
else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60)) else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60))
{ {
sprintf((char*)output_pointer, "%.0f", d); length = sprintf((char*)output_pointer, "%.0f", d);
} }
else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9)) else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9))
{ {
sprintf((char*)output_pointer, "%e", d); length = sprintf((char*)output_pointer, "%e", d);
} }
else else
{ {
sprintf((char*)output_pointer, "%f", d); length = sprintf((char*)output_pointer, "%f", d);
} }
} }
} }
/* sprintf failed */
if (length < 0)
{
return NULL;
}
output_buffer->offset += (size_t)length;
return output_pointer; return output_pointer;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册