-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Fix stack overflow when inspecting circular JSX elements #30021
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 fix is needed in the sister formatter
src/bun.js/test/pretty_format.zig— itscanHaveCircularReferences()(lines 328-330) still omits.JSX, so a circular React element passed to a failingexpect()matcher (e.g.expect(el).toBe(null)) still recurses unboundedly inDiffFormatter→JestPrettyFormatand SIGSEGVs. Mirroring this one-line change there (and ideally also adding.Error/.Event/.Function/.Classfor parity) would close the remaining hole.Extended reasoning...
Summary
This PR fixes the circular-JSX stack overflow in
Bun.inspect()/console.log()by adding.JSXtocanHaveCircularReferences()insrc/bun.js/ConsoleObject.zig. However, Bun has a near-identical second formatter —JestPrettyFormatinsrc/bun.js/test/pretty_format.zig— with its own copy ofcanHaveCircularReferences()that was not updated. That copy still only returnstruefor.Array,.Object,.Map, and.Set, so the exact crash this PR's title promises to fix is still reachable throughexpect()failure output.Code path
JestPrettyFormatis invoked from user code viaDiffFormatter(src/bun.js/test/diff_format.zig:39/48), which renders the "Expected/Received" diff for failing matchers liketoBe,toEqual,toStrictEqual,toHaveProperty,toMatchSnapshot, etc. When the received value is a React element,Tag.get()returns.JSXandprintAs(.JSX, ...)runs.Inside the
.JSXbranch (pretty_format.zig:1477-1700):this.format(...)on the element'skeythis.format(...)on each prop valuethis.format(...)onchildrenThe visited-map
[Circular]short-circuit at lines 876-888 is gated byif (comptime Format.canHaveCircularReferences()). Since.JSXreturnsfalsefromcanHaveCircularReferences()in this file, that guard is compiled out for the JSX path. Additionally, unlikeConsoleObject.zig,pretty_format.zighas nostack_check.isSafeToRecurse()fallback anywhere in the file, so there is no secondary depth limit either.Step-by-step proof
toBefails →DiffFormatteris asked to formatreceived = el.diff_format.zig:39callsJestPrettyFormat.format(..., el, ...).Tag.get(el)sees$$typeof === Symbol.for('react.element')→ returns.JSX.format()enters theif (comptime Format.canHaveCircularReferences())block — skipped, because for.JSXit isfalsein pretty_format.zig.printAs(.JSX, ...)reaches line 1531 and callsthis.format(...)onel.key, which iselitself.The same applies if the cycle goes through
props.foo = elorprops.children = el.Impact
Any test that asserts on a self-referencing React element (or any object graph that contains one) and fails will crash the test runner with a segfault instead of printing a diff. This is the exact crash class described in the PR title and description, just on the
bun:testformatting path rather than theBun.inspectpath.Fix
Mirror the change in
src/bun.js/test/pretty_format.zig:329:For full parity with
ConsoleObject.zigit would also make sense to add.Error,.Event,.Function,.Classwhile you're there, since that file'scanHaveCircularReferences()lags behind on those tags too.