GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix #880 Max recursion depth for cJSON_Duplicate to prevent stack exhaustion by vwvw · Pull Request #888 · DaveGamble/cJSON

Conversation

@vwvw

@vwvw vwvw commented Aug 30, 2024

Copy link
Copy Markdown
Contributor

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 a depth argument. This one is compared to CJSON_CIRCULAR_LIMIT which is currently set to 10'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 the cJSON 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 in cJSON_Delete. I have a similar fix for it too but I'm unsure on what to do once we reach CJSON_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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
vwvw added 2 commits August 30, 2024 13:36
@vwvw
Added max recusrion depth for cJSONDuplicate to prevent stack exhaust…
0719f72
…ion in case of circular reference
@vwvw
Wrong counter increment
4525479
@vwvw
vwvw marked this pull request as ready for review September 3, 2024 10:05
@vwvw

vwvw commented Sep 3, 2024

Copy link
Copy Markdown
Contributor Author

This would fix #880

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vwvw vwvw changed the title Fix #880 Max recusrion depth for cJSON_Duplicate to prevent stack exhaustion Fix #880 Max recursion depth for cJSON_Duplicate to prevent stack exhaustion Sep 4, 2024
PeterAlfredLee
PeterAlfredLee reviewed Sep 14, 2024
View reviewed changes
Comment thread cJSON.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@PeterAlfredLee

PeterAlfredLee commented Sep 18, 2024

Copy link
Copy Markdown
Contributor

Looks good to me for now.

This PR changes the original behavior of cJSON_Duplicate. For nested objects with a depth of over 10,000(which is deep enough IMO), cJSON_Duplicate would stop.
If this makes any inconvenience to anyone, feel free to tell me.

vwvw and Alanscut reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Alanscut
Alanscut merged commit 12c4bf1 into DaveGamble:master Sep 23, 2024
This was referenced Aug 25, 2025
Array Circular Reference Stack Overflow in cJSON: A Systematic Design Issue #954
Open
Stack Overflow in libplist node_estimate_size Function Due to Circular References libimobiledevice/libplist#275
Closed
This was referenced Sep 10, 2025
cjson: add version 1.7.19 conan-io/conan-center-index#28364
Merged
build(deps): Bump cJSON to 1.7.19 atsign-foundation/at_c#616
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@PeterAlfredLee PeterAlfredLee PeterAlfredLee left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@vwvw @PeterAlfredLee @Alanscut
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.