Fix #880 Max recursion depth for cJSON_Duplicate to prevent stack exhaustion#888
Merged
Alanscut merged 2 commits intoDaveGamble:masterfrom Sep 23, 2024
Merged
Fix #880 Max recursion depth for cJSON_Duplicate to prevent stack exhaustion#888Alanscut merged 2 commits intoDaveGamble:masterfrom
Alanscut merged 2 commits intoDaveGamble:masterfrom
Conversation
…ion in case of circular reference
Contributor
Author
|
This would fix #880 |
Contributor
|
Looks good to me for now. This PR changes the original behavior of |
This was referenced Sep 10, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dear maintainers,
Here is a suggestion of a fix to prevent the stack exhaustion in case of circular reference.
I duplicated the
cJSON_Duplicateso that it could take adepthargument. This one is compared toCJSON_CIRCULAR_LIMITwhich is currently set to10'000but likely need adaption based on your knowledge of what is use in practice.I also added a test under
cjson_should_not_follow_too_deep_circular_references.If you don't like the extra function
cJSON_Duplicate_rec, this fix could also be implemented through an additional field in thecJSONstruct (likely break the ABI?) or through a global variable (making cJSON more thread unsafe).What is not yet fixed
In addition to
cJSON_Duplicate, the same issue happens incJSON_Delete. I have a similar fix for it too but I'm unsure on what to do once we reachCJSON_CIRCULAR_LIMIT. Aborting or exiting looks like the correct solution as it is likely that memory corruption occurred through a double free.What would you suggest to do in this case?