From f0c645542d87615705f1255e104d9c0b4d95f889 Mon Sep 17 00:00:00 2001 From: Nicolas Badoux Date: Sun, 25 Aug 2024 23:06:21 +0200 Subject: [PATCH] CJSON_SetValuestring: better test for overlapping string --- tests/misc_tests.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/misc_tests.c b/tests/misc_tests.c index dd8db181..59b3a6b7 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -458,15 +458,19 @@ static void cjson_functions_should_not_crash_with_null_pointers(void) static void cjson_set_valuestring_should_return_null_if_strings_overlap(void) { - cJSON *obj, *obj_dup; + cJSON *obj; char* str; + char* str2; - obj = cJSON_Parse("\"fooz\""); - obj_dup = cJSON_Duplicate(obj, 1); + obj = cJSON_Parse("\"foo0z\""); - str = cJSON_SetValuestring(obj_dup, "beeez"); - cJSON_SetValuestring(obj_dup, str); - cJSON_SetValuestring(obj_dup, ++str); + str = cJSON_SetValuestring(obj, "abcde"); + str += 1; + /* The string passed to strcpy overlap which is not allowed.*/ + str2 = cJSON_SetValuestring(obj, str); + /* If it overlaps, the string will be messed up.*/ + TEST_ASSERT_TRUE(strcmp(str, "bcde") == 0); + TEST_ASSERT_NULL(str2); } static void *CJSON_CDECL failing_realloc(void *pointer, size_t size)