Skip to content

Fix: add depth check to prevent stack overflow in cJSON_Utils (#995) - #1064

Open
tung515 wants to merge 1 commit into
DaveGamble:masterfrom
tung515:fix/merge-patch-depth-limit
Open

Fix: add depth check to prevent stack overflow in cJSON_Utils (#995)#1064
tung515 wants to merge 1 commit into
DaveGamble:masterfrom
tung515:fix/merge-patch-depth-limit

Conversation

@tung515

@tung515 tung515 commented Aug 14, 2026

Copy link
Copy Markdown

Summary

This PR addresses the stack overflow vulnerability reported in #995 caused by uncontrolled recursion when processing deeply nested JSON objects in cJSON_Utils.

It follows the same recursion depth bounding pattern previously introduced in #984 by enforcing CJSON_NESTING_LIMIT.

Changes

  • Recursion depth limits: Added CJSON_NESTING_LIMIT depth checks to merge_patch, generate_merge_patch, and compare_json to safely return failure on deeply nested inputs.
  • Resource cleanup: Ensured detached and allocated nodes are cleanly freed on depth limit failure to prevent memory leaks and double frees.
  • Unit tests: Added unit tests in tests/misc_utils_tests.c to verify that cJSONUtils_MergePatch and cJSONUtils_GenerateMergePatch reject deeply nested structures safely.

Related Issue

Fixes #995

…veGamble#995)

- Add CJSON_NESTING_LIMIT depth check to merge_patch, generate_merge_patch, and compare_json.
- Prevent stack exhaustion when processing deeply nested JSON structures.
- Add unit tests in misc_utils_tests to verify safety against deep recursion.
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.

[Security] Stack Overflow via Uncontrolled Recursion in cJSONUtils_MergePatch

1 participant