From bfdbf5195db811e8575a3c0d22f74b7a460bc695 Mon Sep 17 00:00:00 2001 From: thomas Date: Wed, 28 Feb 2018 12:39:44 -0500 Subject: [PATCH] if end pointer non null, assumes end of string --- cJSON.c | 6 +++++- tests/CMakeLists.txt | 1 + tests/parse_memory.c | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/parse_memory.c diff --git a/cJSON.c b/cJSON.c index 3dcde3d2..3d97d4e8 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1013,7 +1013,11 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return } buffer.content = (const unsigned char*)value; - buffer.length = strlen((const char*)value) + sizeof(""); + if(return_parse_end && *return_parse_end){ + buffer.length = (size_t)(*return_parse_end - value); + }else{ + buffer.length = strlen((const char*)value) + sizeof(""); + } buffer.offset = 0; buffer.hooks = global_hooks; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 79672927..ca7285d6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -42,6 +42,7 @@ if(ENABLE_CJSON_TEST) set(unity_tests parse_examples parse_number + parse_memory parse_hex4 parse_string parse_array diff --git a/tests/parse_memory.c b/tests/parse_memory.c new file mode 100644 index 00000000..d3708f16 --- /dev/null +++ b/tests/parse_memory.c @@ -0,0 +1,26 @@ +#include +#include +#include + +#include "common.h" +#include "unity/examples/unity_config.h" +#include "unity/src/unity.h" + +static void parse_memory_should_parse_true(void) { + cJSON *item; + const char *mem = + "{" + "\"hello\":\"world\"" + "}", + *end = mem + strlen(mem); + item = cJSON_ParseWithOpts(mem, &end, false); + TEST_ASSERT_NOT_NULL(item); + cJSON_Delete(item); +} + +int main(void) { + /* initialize cJSON item */ + UNITY_BEGIN(); + RUN_TEST(parse_memory_should_parse_true); + return UNITY_END(); +}