Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cJSON_Utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -788,15 +788,16 @@ static void overwrite_item(cJSON * const root, const cJSON replacement)
return;
}

if (root->string != NULL)
/* only free what root owns, mirroring cJSON_Delete */
if (!(root->type & cJSON_StringIsConst) && (root->string != NULL))
{
cJSON_free(root->string);
}
if (root->valuestring != NULL)
if (!(root->type & cJSON_IsReference) && (root->valuestring != NULL))
{
cJSON_free(root->valuestring);
}
if (root->child != NULL)
if (!(root->type & cJSON_IsReference) && (root->child != NULL))
{
cJSON_Delete(root->child);
}
Expand Down Expand Up @@ -875,8 +876,12 @@ static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_
/* the string "value" isn't needed */
if (object->string != NULL)
{
cJSON_free(object->string);
if (!(object->type & cJSON_StringIsConst))
{
cJSON_free(object->string);
}
object->string = NULL;
object->type &= ~cJSON_StringIsConst;
}

status = 0;
Expand Down
86 changes: 86 additions & 0 deletions tests/misc_utils_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,97 @@ static void cjson_utils_functions_shouldnt_crash_with_null_pointers(void)
cJSON_Delete(item);
}

static void apply_root_patch(cJSON *target, const char *patch_json)
{
cJSON *patch = cJSON_Parse(patch_json);
TEST_ASSERT_NOT_NULL(patch);
TEST_ASSERT_EQUAL_INT(0, cJSONUtils_ApplyPatchesCaseSensitive(target, patch));
cJSON_Delete(patch);
}

static void cjson_utils_replace_root_should_not_free_referenced_child(void)
{
cJSON *shared = cJSON_Parse("{\"k\":\"v\"}");
cJSON *reference = cJSON_CreateObjectReference(shared);
TEST_ASSERT_NOT_NULL(reference);

apply_root_patch(reference, "[{\"op\":\"replace\",\"path\":\"\",\"value\":1}]");
TEST_ASSERT_TRUE(cJSON_IsNumber(reference));
TEST_ASSERT_EQUAL_STRING("v", cJSON_GetObjectItem(shared, "k")->valuestring);

cJSON_Delete(reference);
cJSON_Delete(shared);
}

static void cjson_utils_remove_root_should_not_free_referenced_child(void)
{
cJSON *shared = cJSON_Parse("[1,2]");
cJSON *reference = cJSON_CreateArrayReference(shared);
TEST_ASSERT_NOT_NULL(reference);

apply_root_patch(reference, "[{\"op\":\"remove\",\"path\":\"\"}]");
TEST_ASSERT_EQUAL_INT(2, cJSON_GetArraySize(shared));

cJSON_Delete(reference);
cJSON_Delete(shared);
}

static void cjson_utils_replace_root_should_not_free_referenced_valuestring(void)
{
static char owned_by_caller[] = "owned";
cJSON *reference = cJSON_CreateStringReference(owned_by_caller);
TEST_ASSERT_NOT_NULL(reference);

apply_root_patch(reference, "[{\"op\":\"replace\",\"path\":\"\",\"value\":1}]");
TEST_ASSERT_TRUE(cJSON_IsNumber(reference));
TEST_ASSERT_EQUAL_STRING("owned", owned_by_caller);

cJSON_Delete(reference);
}

static void cjson_utils_replace_root_should_not_free_constant_key(void)
{
cJSON *container = cJSON_CreateObject();
cJSON *member = NULL;
cJSON_AddItemToObjectCS(container, "cfg", cJSON_CreateString("x"));
member = cJSON_GetObjectItem(container, "cfg");
TEST_ASSERT_NOT_NULL(member);

apply_root_patch(member, "[{\"op\":\"replace\",\"path\":\"\",\"value\":2}]");
TEST_ASSERT_TRUE(cJSON_IsNumber(member));

cJSON_Delete(container);
}

static void cjson_utils_replace_root_should_not_free_constant_value_key(void)
{
cJSON *document = cJSON_Parse("{\"x\":1}");
cJSON *patches = cJSON_CreateArray();
cJSON *operation = cJSON_CreateObject();
cJSON_AddItemToObject(operation, "op", cJSON_CreateString("replace"));
cJSON_AddItemToObject(operation, "path", cJSON_CreateString(""));
cJSON_AddItemToObjectCS(operation, "value", cJSON_CreateNumber(1));
cJSON_AddItemToArray(patches, operation);

TEST_ASSERT_EQUAL_INT(0, cJSONUtils_ApplyPatchesCaseSensitive(document, patches));
TEST_ASSERT_TRUE(cJSON_IsNumber(document));
TEST_ASSERT_NULL(document->string);
TEST_ASSERT_FALSE(document->type & cJSON_StringIsConst);

cJSON_Delete(patches);
cJSON_Delete(document);
}

int main(void)
{
UNITY_BEGIN();

RUN_TEST(cjson_utils_functions_shouldnt_crash_with_null_pointers);
RUN_TEST(cjson_utils_replace_root_should_not_free_referenced_child);
RUN_TEST(cjson_utils_remove_root_should_not_free_referenced_child);
RUN_TEST(cjson_utils_replace_root_should_not_free_referenced_valuestring);
RUN_TEST(cjson_utils_replace_root_should_not_free_constant_key);
RUN_TEST(cjson_utils_replace_root_should_not_free_constant_value_key);

return UNITY_END();
}