Skip to content

Fix assertion failure in Bun.plugin when target coercion throws#29589

Closed
robobun wants to merge 1 commit into
mainfrom
farm/9b2787d1/fix-plugin-target-symbol
Closed

Fix assertion failure in Bun.plugin when target coercion throws#29589
robobun wants to merge 1 commit into
mainfrom
farm/9b2787d1/fix-plugin-target-symbol

Conversation

@robobun

@robobun robobun commented Apr 22, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Fixes a debug assertion failure (assertNoException) in Bun.plugin() when the target option is a value whose string coercion throws — e.g. a Symbol, or an object with a throwing Symbol.toPrimitive.

toStringOrNull() returns nullptr with a pending exception in these cases. The surrounding code only handled the non-null branch and fell through to constructEmptyObject() with the exception still pending, which trips ExceptionScope::assertNoException() in debug/ASAN builds.

Found by Fuzzilli (fingerprint 15b3dd05628c2f45).

How did you verify your code works?

Minimal repro that asserted before and now throws a clean TypeError:

Bun.plugin({
  target: Symbol("foo"),
  setup() {},
});

Added regression tests to test/js/bun/plugin/plugins.test.ts covering both the Symbol case and a throwing Symbol.toPrimitive. Full plugins.test.ts suite passes (31 pass, 1 todo).

When the target option passed to Bun.plugin() was a Symbol or an object
whose string coercion throws, toStringOrNull() would return null with a
pending exception. The code would then fall through to constructEmptyObject
with the exception still pending, tripping assertNoException() in debug
builds.
@robobun

robobun commented Apr 22, 2026

Copy link
Copy Markdown
Collaborator Author
Updated 8:05 AM PT - Apr 22nd, 2026

@robobun, your commit a6e3948 has some failures in Build #47218 (All Failures)


🧪   To try this PR locally:

bunx bun-pr 29589

That installs a local version of the PR into your bun-29589 executable, so you can run:

bun-29589 --bun

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — straightforward exception-scope fix following the established RETURN_IF_EXCEPTION pattern, with regression tests.

Extended reasoning...

Overview

This PR adds two RETURN_IF_EXCEPTION checks in setupBunPlugin() (src/bun.js/bindings/BunPlugin.cpp) to handle the case where toStringOrNull() on the target option returns null with a pending exception (e.g., Symbol or a throwing Symbol.toPrimitive). It also adds two regression tests to plugins.test.ts.

Security risks

None. This only adds early-return guards on an existing throw scope; no new inputs, no auth/crypto/permission surface.

Level of scrutiny

Low. The change is a 2-line mechanical application of the standard JSC RETURN_IF_EXCEPTION(throwScope, {}) pattern already used dozens of times in the same file. The fix is purely defensive — it converts a debug-only assertion failure into a properly propagated JS exception, with no behavior change on the happy path.

Other factors

  • The bug hunting system found no issues.
  • The added tests directly cover both the Symbol case and the throwing-coercion case, and the PR description confirms the full plugins.test.ts suite passes.
  • The first inner check after ->value(globalObject) is slightly redundant in practice but harmless and consistent with surrounding code; the outer check is the actual fix and is correctly placed to catch the null-return-with-exception path before constructEmptyObject().

@coderabbitai

coderabbitai Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0771cbf4-26f7-4dab-b7be-7f6724452054

📥 Commits

Reviewing files that changed from the base of the PR and between 3453c22 and a6e3948.

📒 Files selected for processing (2)
  • src/bun.js/bindings/BunPlugin.cpp
  • test/js/bun/plugin/plugins.test.ts

Walkthrough

Added exception handling in setupBunPlugin to catch errors during target value string conversion, with corresponding test cases validating that Symbol targets and failed primitive coercions throw appropriate errors instead of causing crashes.

Changes

Cohort / File(s) Summary
Plugin target coercion exception handling
src/bun.js/bindings/BunPlugin.cpp, test/js/bun/plugin/plugins.test.ts
Introduced RETURN_IF_EXCEPTION checks after string conversion of targetValue in the plugin setup path. Added test cases validating that Symbol targets and [Symbol.toPrimitive] throwing implementations result in proper error propagation.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main bug fix: handling assertion failures when target coercion throws in Bun.plugin, which aligns with the code changes and test additions.
Description check ✅ Passed The description follows the template with both required sections complete: explains what the PR does (fixes assertion failure with string coercion) and how verification was done (minimal repro, regression tests added).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown
Contributor

This PR may be a duplicate of:

  1. Handle exceptions from toStringOrNull in Bun.plugin target parsing #28369 - Also adds exception handling guards for toStringOrNull in setupBunPlugin() when Bun.plugin target coercion throws

🤖 Generated with Claude Code

@robobun

robobun commented Apr 22, 2026

Copy link
Copy Markdown
Collaborator Author

Duplicate of #28369 which has the identical fix.

@robobun robobun closed this Apr 22, 2026
@robobun robobun deleted the farm/9b2787d1/fix-plugin-target-symbol branch April 22, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant