Skip to content

fix: prevent NULL valuestring dereference in cJSONUtils_ApplyPatches#1033

Open
dalisyron wants to merge 1 commit into
DaveGamble:masterfrom
dalisyron:fix/patch-null-valuestring
Open

fix: prevent NULL valuestring dereference in cJSONUtils_ApplyPatches#1033
dalisyron wants to merge 1 commit into
DaveGamble:masterfrom
dalisyron:fix/patch-null-valuestring

Conversation

@dalisyron

Copy link
Copy Markdown

Description

cJSONUtils_ApplyPatches/...CaseSensitive can crash with a NULL pointer dereference when a patch contains a cJSON_String field whose valuestring is NULL, instead of rejecting the malformed patch.

Root cause

The patch code validates the op, path and from fields with cJSON_IsString(), which only checks the type tag ((type & 0xFF) == cJSON_String) and does not guarantee valuestring != NULL. A string item with a NULL valuestring is reachable through the public API (e.g. cJSON_CreateStringReference(NULL)), so it passes the check and is then dereferenced:

  • decode_patch_operation()strcmp(operation->valuestring, ...)
  • apply_patch()path->valuestring[0] / detach_path(..., path->valuestring)
  • apply_patch()detach_path() / get_item_from_pointer(..., from->valuestring)

This continues the hardening from #1006, which tightened the same from check from == NULL to !cJSON_IsString(from).

Fix

Reject a NULL valuestring alongside the existing cJSON_IsString() checks for op, path and from, so the affected patches return the existing malformed-patch error codes (3 / 2 / 4) instead of crashing — consistent with how other invalid patches are already handled.

Verification

  • Added a regression test cjson_utils_apply_patches_should_reject_null_valuestring in tests/misc_utils_tests.c covering all three fields (the three PoCs from the issue). It crashes (NULL deref) on master and passes with this change.
  • Ran the full suite locally across the CI matrix — GCC and Clang × {ENABLE_SANITIZERS, ENABLE_VALGRIND, none}: 22/22 tests pass, 0 warnings under -std=c89 -pedantic -Werror -Wconversion, and valgrind reports no leaks/errors.

Fixes #1011

cJSON_IsString() only checks the type tag, so a cJSON_String item whose
valuestring is NULL (e.g. one created with cJSON_CreateStringReference(NULL))
passes the check. The JSON patch code then dereferences the "op", "path"
and "from" valuestrings via strcmp()/indexing/strdup(), causing a NULL
pointer dereference instead of rejecting the malformed patch.

Reject a NULL valuestring alongside the existing cJSON_IsString() checks so
these patches return the usual malformed-patch error codes, matching how
other invalid patches are handled.

Fixes DaveGamble#1011
@dalisyron dalisyron force-pushed the fix/patch-null-valuestring branch from 02d632d to d91a728 Compare June 8, 2026 21:12
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.

cJSON_Utils may dereference NULL valuestring after cJSON_IsString() checks

1 participant