Skip to content

Commit

Permalink
CJSON_SetValuestring: better test for overlapping string
Browse files Browse the repository at this point in the history
  • Loading branch information
vwvw committed Aug 25, 2024
1 parent 193cd6a commit f0c6455
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions tests/misc_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f0c6455

Please sign in to comment.