Просмотр исходного кода

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.
Max Bruckner 7 лет назад
Родитель
Сommit
a0aa2df75a
4 измененных файлов с 18 добавлено и 32 удалено
  1. 10 18
      cJSON.c
  2. 2 2
      cJSON.h
  3. 5 11
      tests/configuration_tests.c
  4. 1 1
      tests/misc_tests.c

+ 10 - 18
cJSON.c

@@ -126,7 +126,7 @@ typedef struct internal_configuration
     cJSON_bool case_sensitive;
     cJSON_Allocators allocators;
     void *userdata;
-    size_t *end_position;
+    size_t end_position;
 } internal_configuration;
 
 #if defined(_MSC_VER)
@@ -204,7 +204,7 @@ static void deallocate(const internal_configuration * const configuration, void
         realloc_wrapper\
     },\
     NULL, /* no userdata */\
-    NULL /* no end position */\
+    0 /* default end position */\
 }
 
 /* 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. */
-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 };
     cJSON *item = NULL;
@@ -1102,10 +1102,8 @@ static cJSON *parse(const char * const json, const internal_configuration * cons
             goto fail;
         }
     }
-    if (configuration->end_position != NULL)
-    {
-        *configuration->end_position = buffer.offset;
-    }
+
+    configuration->end_position = buffer.offset;
 
     return item;
 
@@ -1130,10 +1128,7 @@ fail:
             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;
     }
 
@@ -1143,17 +1138,15 @@ fail:
 /* 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)
 {
-    size_t end_position = 0;
     internal_configuration configuration = global_configuration;
     cJSON *item = NULL;
 
     configuration.allow_data_after_json = !require_null_terminated;
-    configuration.end_position = &end_position;
     item = parse(json, &configuration);
 
     if (return_parse_end != NULL)
     {
-        *return_parse_end = json + end_position;
+        *return_parse_end = json + configuration.end_position;
     }
 
     return item;
@@ -2955,15 +2948,14 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Config
     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)
     {
-        return NULL;
+        return 0;
     }
 
-    ((internal_configuration*)configuration)->end_position = parse_end;
-    return configuration;
+    return ((internal_configuration*)configuration)->end_position;
 }
 
 CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size)

+ 2 - 2
cJSON.h

@@ -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);
 /* Change the allocator userdata attached to a cJSON_Configuration */
 CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Configuration configuration, void *userdata);
-/* Change the pointer where the end of parsing is written to */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeParseEnd(cJSON_Configuration configuration, size_t * const parse_end);
+/* Get the position relative to the JSON where the parser stopped, return 0 if invalid. */
+CJSON_PUBLIC(size_t) cJSON_ConfigurationGetParseEnd(cJSON_Configuration configuration);
 /* Set how many bytes should be initially allocated for printing */
 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;

+ 5 - 11
tests/configuration_tests.c

@@ -129,18 +129,12 @@ static void configuration_change_userdata_should_change_userdata(void)
     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 = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    configuration = (internal_configuration*)cJSON_ConfigurationChangeParseEnd(configuration, &end_position);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    TEST_ASSERT_TRUE_MESSAGE(configuration->end_position == &end_position, "Failed to set parse end.");
+    internal_configuration configuration = global_default_configuration;
+    configuration.end_position = 42;
 
-    free(configuration);
+    TEST_ASSERT_EQUAL_MESSAGE(cJSON_ConfigurationGetParseEnd(&configuration), 42, "Failed to get parse end.");
 }
 
 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_not_change_incomplete_allocators);
     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_not_allow_empty_sizes);
     RUN_TEST(configuration_change_format_should_change_format);

+ 1 - 1
tests/misc_tests.c

@@ -420,7 +420,7 @@ static void *failing_realloc(void *pointer, size_t size, void *userdata)
 
 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.buffer = (unsigned char*)malloc(100);
     TEST_ASSERT_NOT_NULL(buffer.buffer);