From d441fa05b3f012d5d9c1af1ff6886154aaf6eef8 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Fri, 3 Mar 2017 00:05:11 +0100 Subject: [PATCH] print_value: return boolean instead of pointer --- cJSON.c | 21 +++++++++++++-------- tests/print_value.c | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cJSON.c b/cJSON.c index c780b77..675ce2f 100644 --- a/cJSON.c +++ b/cJSON.c @@ -813,7 +813,7 @@ static unsigned char *print_string(const cJSON * const item, printbuffer * const /* Predeclare these prototypes. */ static const unsigned char *parse_value(cJSON * const item, const unsigned char * const input, const unsigned char ** const ep, const internal_hooks * const hooks); -static unsigned char *print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); +static cJSON_bool print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); static const unsigned char *parse_array(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks); static unsigned char *print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks); static const unsigned char *parse_object(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks); @@ -893,7 +893,7 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i } /* print the value */ - if (print_value(item, 0, format, buffer, hooks) == NULL) + if (!print_value(item, 0, format, buffer, hooks)) { goto fail; } @@ -957,7 +957,12 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON p.offset = 0; p.noalloc = false; - return (char*)print_value(item, 0, fmt, &p, &global_hooks); + if (!print_value(item, 0, fmt, &p, &global_hooks)) + { + return NULL; + } + + return (char*)p.buffer; } CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt) @@ -973,7 +978,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const i p.length = (size_t)len; p.offset = 0; p.noalloc = true; - return print_value(item, 0, fmt, &p, &global_hooks) != NULL; + return print_value(item, 0, fmt, &p, &global_hooks); } /* Parser core - when encountering text, process appropriately. */ @@ -1031,13 +1036,13 @@ static const unsigned char *parse_value(cJSON * const item, const unsigned char } /* Render a value to text. */ -static unsigned char *print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks) { unsigned char *output = NULL; if ((item == NULL) || (output_buffer == NULL)) { - return NULL; + return false; } switch ((item->type) & 0xFF) @@ -1101,7 +1106,7 @@ static unsigned char *print_value(const cJSON * const item, const size_t depth, break; } - return output; + return output != NULL; } /* Build an array from input text. */ @@ -1210,7 +1215,7 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth, while (current_element != NULL) { - if (print_value(current_element, depth + 1, format, output_buffer, hooks) == NULL) + if (!print_value(current_element, depth + 1, format, output_buffer, hooks)) { return NULL; } diff --git a/tests/print_value.c b/tests/print_value.c index 41ee286..7e0f52f 100644 --- a/tests/print_value.c +++ b/tests/print_value.c @@ -43,7 +43,7 @@ static void assert_print_value(const char *input) TEST_ASSERT_NOT_NULL_MESSAGE(parse_value(item, (const unsigned char*)input, &error_pointer, &global_hooks), "Failed to parse value."); - TEST_ASSERT_NOT_NULL_MESSAGE(print_value(item, 0, false, &buffer, &global_hooks), "Failed to print value."); + TEST_ASSERT_TRUE_MESSAGE(print_value(item, 0, false, &buffer, &global_hooks), "Failed to print value."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, buffer.buffer, "Printed value is not as expected."); reset(item);