Skip to content

Fix NULL dereference in cJSONUtils_GenerateMergePatch on keyless members - #1147

Open
xhon-pelushi wants to merge 1 commit into
DaveGamble:masterfrom
xhon-pelushi:fix-1143-merge-patch-null-key
Open

xhon-pelushi wants to merge 1 commit into
DaveGamble:masterfrom
xhon-pelushi:fix-1143-merge-patch-null-key

Conversation

@xhon-pelushi

Copy link
Copy Markdown

Fixes #1143.

generate_merge_patch compared object keys with a plain strcmp, so cJSONUtils_GenerateMergePatch / cJSONUtils_GenerateMergePatchCaseSensitive crashed when either object had a member without a key (easy to get with cJSON_AddItemToArray on an object). It now uses compare_strings, the same helper create_patches and sort_object already use, which handles NULL keys and also follows case_sensitive so the comparison matches the sort order.

A keyless member can't be written into a merge patch, so cJSON_AddItemToObject rejects it. The item that was created for it was leaked before; it is now freed and the member is skipped.

Added cjson_utils_generate_merge_patch_should_not_crash_on_keyless_members to tests/misc_utils_tests.c. With -DENABLE_SANITIZERS=On it fails without the fix (runtime error: null pointer passed as argument 1 at the strcmp) and passes with it. ctest 22/22 with sanitizers, and the plain make build is warning-free.

generate_merge_patch compared object keys with a plain strcmp, so an
object member without a key (e.g. added with cJSON_AddItemToArray)
crashed it. Use compare_strings like create_patches does, which also
respects case_sensitive, and free the item when it can't be added to
the patch because it has no key.

Fixes DaveGamble#1143
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NULL dereference in cJSONUtils_GenerateMergePatch on object members with a NULL key

1 participant