Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if end pointer non null, assumes end of string #247

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion cJSON.c
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Expand Up @@ -42,6 +42,7 @@ if(ENABLE_CJSON_TEST)
set(unity_tests
parse_examples
parse_number
parse_memory
parse_hex4
parse_string
parse_array
Expand Down
26 changes: 26 additions & 0 deletions tests/parse_memory.c
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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();
}