]> git.feebdaed.xyz Git - 0xmirror/cJSON.git/commitdiff
CJSON_SetValuestring: better test for overlapping string
authorNicolas Badoux <n.badoux@hotmail.com>
Sun, 25 Aug 2024 21:06:21 +0000 (23:06 +0200)
committerAlan Wang <wp_scut@163.com>
Fri, 30 Aug 2024 03:29:28 +0000 (11:29 +0800)
tests/misc_tests.c

index b10c0a05581c82a679108ee6abfc529206110dd0..e1187f836141de6d9571de04b7991c1312e37bbe 100644 (file)
@@ -473,15 +473,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)