Fix #880 Max recursion depth for cJSON_Duplicate to prevent stack exhaustion #888
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_Duplicate
so that it could take adepth
argument. This one is compared toCJSON_CIRCULAR_LIMIT
which is currently set to10'000
but 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 thecJSON
struct (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?