-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix stack overflow when inspecting JSX elements with circular references #29750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 The same crash still exists in the parallel jest pretty-formatter:
src/bun.js/test/pretty_format.zig:328-330has its owncanHaveCircularReferences()that still omits.JSX, and its.JSXbranch recurses intokey/props/childrenviathis.format()with no stack-check fallback. After this PR,expect(circularJsxEl).toMatchSnapshot()or a failingtoEqualdiff (viadiff_format.zig) will still segfault — the fuzzer crash is only half-fixed. The same one-line change should be mirrored atpretty_format.zig:329.Extended reasoning...
What the bug is
This PR correctly adds
.JSXtocanHaveCircularReferences()insrc/bun.js/ConsoleObject.zig:1139so thatBun.inspect()/console.log()no longer stack-overflow on circular JSX elements. However, Bun has a second, parallel formatter insrc/bun.js/test/pretty_format.zig(JestPrettyFormat) with its own copy ofcanHaveCircularReferences()at lines 328-330:This copy still does not include
.JSX, and unlikeConsoleObject.zigit has nostack_check.isSafeToRecurse()fallback either.The code path that triggers it
JestPrettyFormat's.JSXbranch inprintAs()(line 1477+) is structurally identical to the one this PR fixes:keyviathis.format(...)this.format(...)this.format(...)In
format()(line ~876), the visited-map circular-reference tracking is gated oncomptime canHaveCircularReferences(tag). Since.JSXreturnsfalse, no visited-set entry is recorded and the recursion is unbounded.This formatter is reachable from user code via:
expect(...).toMatchSnapshot()→expect.zig:819→jestSnapshotPrettyFormat→JestPrettyFormat.formatexpect(...).toEqual(...)/.toBe(...)diff →diff_format.zig:39,48→JestPrettyFormat.formatWhy existing code doesn't prevent it
Tag.get()inpretty_format.zig(lines 416-423) detects25618typeof === Symbol.for('react.element')and returns.JSXexactly likeConsoleObject.zigdoes, so the value is routed into the unprotected.JSXbranch rather than the protected.Objectbranch. Grep confirms there is noisSafeToRecurse/StackCheckguard anywhere inpretty_format.zig, so there is no secondary safety net.Step-by-step proof
toMatchSnapshotcallsjestSnapshotPrettyFormat(el)(expect.zig:819) →JestPrettyFormat.format(el).Tag.get(el)sees$$typeof === react.element→ returns.JSX(pretty_format.zig:416-423).format()checkscanHaveCircularReferences(.JSX)→false→ skips visited-map insert.printAs(.JSX, ...)iterates props, hitsfoo, callsthis.format(el)at line 1580.The same applies with
el.key = el(line 1531) orel.props.children = [el](line 1643).Impact
The fuzzer crash (fingerprint
bb8715595a137556) that this PR targets is only half-fixed:Bun.inspect()is safe, but the test-runner pretty-formatter for the same value still crashes the process. Any user who snapshot-tests or diff-compares a React tree containing a back-reference (e.g., a parent ref stashed on a child) will hit a hard segfault inbun test.Fix
Mirror the change at
src/bun.js/test/pretty_format.zig:329— add.JSXto the switch (and optionally.Error/.Event/.Function/.Classfor parity withConsoleObject.zig). It's the same one-token diff.