cJSON_ConfigurationChangeParseEnd -> cJSON_ConfigurationGetParseEnd

This is probably a better approach than potentially having a pointer
that points to garbage on the stack and gets written to by cJSON.
This commit is contained in:
Max Bruckner 2018-02-03 04:28:44 +01:00
parent ba81437601
commit a0aa2df75a
4 changed files with 18 additions and 32 deletions

28
cJSON.c
View File

@ -126,7 +126,7 @@ typedef struct internal_configuration
cJSON_bool case_sensitive; cJSON_bool case_sensitive;
cJSON_Allocators allocators; cJSON_Allocators allocators;
void *userdata; void *userdata;
size_t *end_position; size_t end_position;
} internal_configuration; } internal_configuration;
#if defined(_MSC_VER) #if defined(_MSC_VER)
@ -204,7 +204,7 @@ static void deallocate(const internal_configuration * const configuration, void
realloc_wrapper\ realloc_wrapper\
},\ },\
NULL, /* no userdata */\ NULL, /* no userdata */\
NULL /* no end position */\ 0 /* default end position */\
} }
/* this is necessary to assign the default configuration after initialization */ /* this is necessary to assign the default configuration after initialization */
@ -1063,7 +1063,7 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
} }
/* Parse an object - create a new root, and populate. */ /* Parse an object - create a new root, and populate. */
static cJSON *parse(const char * const json, const internal_configuration * const configuration) static cJSON *parse(const char * const json, internal_configuration * const configuration)
{ {
parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
cJSON *item = NULL; cJSON *item = NULL;
@ -1102,10 +1102,8 @@ static cJSON *parse(const char * const json, const internal_configuration * cons
goto fail; goto fail;
} }
} }
if (configuration->end_position != NULL)
{ configuration->end_position = buffer.offset;
*configuration->end_position = buffer.offset;
}
return item; return item;
@ -1130,10 +1128,7 @@ fail:
local_error.position = buffer.length - 1; local_error.position = buffer.length - 1;
} }
if (configuration->end_position != NULL) configuration->end_position = local_error.position;
{
*configuration->end_position = local_error.position;
}
global_error = local_error; global_error = local_error;
} }
@ -1143,17 +1138,15 @@ fail:
/* Parse an object - create a new root, and populate. */ /* Parse an object - create a new root, and populate. */
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *json, const char **return_parse_end, cJSON_bool require_null_terminated) CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *json, const char **return_parse_end, cJSON_bool require_null_terminated)
{ {
size_t end_position = 0;
internal_configuration configuration = global_configuration; internal_configuration configuration = global_configuration;
cJSON *item = NULL; cJSON *item = NULL;
configuration.allow_data_after_json = !require_null_terminated; configuration.allow_data_after_json = !require_null_terminated;
configuration.end_position = &end_position;
item = parse(json, &configuration); item = parse(json, &configuration);
if (return_parse_end != NULL) if (return_parse_end != NULL)
{ {
*return_parse_end = json + end_position; *return_parse_end = json + configuration.end_position;
} }
return item; return item;
@ -2955,15 +2948,14 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Config
return configuration; return configuration;
} }
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeParseEnd(cJSON_Configuration configuration, size_t * const parse_end) CJSON_PUBLIC(size_t) cJSON_ConfigurationGetParseEnd(cJSON_Configuration configuration)
{ {
if (configuration == NULL) if (configuration == NULL)
{ {
return NULL; return 0;
} }
((internal_configuration*)configuration)->end_position = parse_end; return ((internal_configuration*)configuration)->end_position;
return configuration;
} }
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size) CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size)

View File

@ -158,8 +158,8 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON_Allocato
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeAllocators(cJSON_Configuration configuration, const cJSON_Allocators allocators); CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeAllocators(cJSON_Configuration configuration, const cJSON_Allocators allocators);
/* Change the allocator userdata attached to a cJSON_Configuration */ /* Change the allocator userdata attached to a cJSON_Configuration */
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Configuration configuration, void *userdata); CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Configuration configuration, void *userdata);
/* Change the pointer where the end of parsing is written to */ /* Get the position relative to the JSON where the parser stopped, return 0 if invalid. */
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeParseEnd(cJSON_Configuration configuration, size_t * const parse_end); CJSON_PUBLIC(size_t) cJSON_ConfigurationGetParseEnd(cJSON_Configuration configuration);
/* Set how many bytes should be initially allocated for printing */ /* Set how many bytes should be initially allocated for printing */
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size); CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size);
typedef enum { CJSON_FORMAT_MINIFIED = 0, CJSON_FORMAT_DEFAULT = 1 } cJSON_Format; typedef enum { CJSON_FORMAT_MINIFIED = 0, CJSON_FORMAT_DEFAULT = 1 } cJSON_Format;

View File

@ -129,18 +129,12 @@ static void configuration_change_userdata_should_change_userdata(void)
free(configuration); free(configuration);
} }
static void configuration_change_parse_end_should_change_parse_end(void) static void configuration_get_parse_end_should_get_the_parse_end(void)
{ {
size_t end_position = 0; internal_configuration configuration = global_default_configuration;
internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL); configuration.end_position = 42;
TEST_ASSERT_NOT_NULL(configuration);
configuration = (internal_configuration*)cJSON_ConfigurationChangeParseEnd(configuration, &end_position); TEST_ASSERT_EQUAL_MESSAGE(cJSON_ConfigurationGetParseEnd(&configuration), 42, "Failed to get parse end.");
TEST_ASSERT_NOT_NULL(configuration);
TEST_ASSERT_TRUE_MESSAGE(configuration->end_position == &end_position, "Failed to set parse end.");
free(configuration);
} }
static void configuration_change_prebuffer_size_should_change_buffer_size(void) static void configuration_change_prebuffer_size_should_change_buffer_size(void)
@ -220,7 +214,7 @@ int main(void)
RUN_TEST(configuration_change_allocators_should_change_allocators); RUN_TEST(configuration_change_allocators_should_change_allocators);
RUN_TEST(configuration_change_allocators_should_not_change_incomplete_allocators); RUN_TEST(configuration_change_allocators_should_not_change_incomplete_allocators);
RUN_TEST(configuration_change_userdata_should_change_userdata); RUN_TEST(configuration_change_userdata_should_change_userdata);
RUN_TEST(configuration_change_parse_end_should_change_parse_end); RUN_TEST(configuration_get_parse_end_should_get_the_parse_end);
RUN_TEST(configuration_change_prebuffer_size_should_change_buffer_size); RUN_TEST(configuration_change_prebuffer_size_should_change_buffer_size);
RUN_TEST(configuration_change_prebuffer_size_should_not_allow_empty_sizes); RUN_TEST(configuration_change_prebuffer_size_should_not_allow_empty_sizes);
RUN_TEST(configuration_change_format_should_change_format); RUN_TEST(configuration_change_format_should_change_format);

View File

@ -420,7 +420,7 @@ static void *failing_realloc(void *pointer, size_t size, void *userdata)
static void ensure_should_fail_on_failed_realloc(void) static void ensure_should_fail_on_failed_realloc(void)
{ {
printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, true, true, {malloc_wrapper, free_wrapper, failing_realloc}, NULL, NULL } }; printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, true, true, {malloc_wrapper, free_wrapper, failing_realloc}, NULL, 0 } };
buffer.configuration.userdata = &buffer; buffer.configuration.userdata = &buffer;
buffer.buffer = (unsigned char*)malloc(100); buffer.buffer = (unsigned char*)malloc(100);
TEST_ASSERT_NOT_NULL(buffer.buffer); TEST_ASSERT_NOT_NULL(buffer.buffer);