Skip to content

glob: fix allocator mismatch in finalize and remove dead code - #29381

Merged
alii merged 1 commit into
mainfrom
ali/glob-dead-code
Apr 17, 2026
Merged

glob: fix allocator mismatch in finalize and remove dead code#29381
alii merged 1 commit into
mainfrom
ali/glob-dead-code

Conversation

@alii

@alii alii commented Apr 16, 2026

Copy link
Copy Markdown
Member

The Glob struct and its pattern string were allocated with bun.default_allocator but freed with VirtualMachine.get().allocator (a mimalloc arena). This works today only because mimalloc's mi_free ignores heap ownership, but is a contract violation that breaks under heap_breakdown or future allocator changes.

  • Use bun.new/bun.destroy for the Glob struct.
  • Free pattern with bun.default_allocator to match toSliceClone.
  • Drop the unnecessary @constCast.
  • Remove dead pattern_codepoints field and convertUtf8 function (no callers anywhere in the tree).
  • Fix debugPatternComopnents typo.

The Glob struct and its pattern string were allocated with
bun.default_allocator but freed with VirtualMachine.get().allocator (a
mimalloc arena). This works today only because mimalloc's mi_free
ignores heap ownership, but is a contract violation that breaks under
heap_breakdown or future allocator changes.

- Use bun.new/bun.destroy for the Glob struct (canonical helpers).
- Free pattern with bun.default_allocator to match toSliceClone.
- Drop the unnecessary @constcast.
- Remove dead pattern_codepoints field and convertUtf8 function (no
  callers anywhere in the tree).
- Fix debugPatternComopnents typo.
@robobun

robobun commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator
Updated 6:42 PM PT - Apr 16th, 2026

@alii, your commit 41e104b has 5 failures in Build #45948 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 29381

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

bun-29381 --bun

@coderabbitai

coderabbitai Bot commented Apr 16, 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: 478b637f-ef0f-4a4d-bb7e-3f4690582980

📥 Commits

Reviewing files that changed from the base of the PR and between b2d3f5d and 41e104b.

📒 Files selected for processing (2)
  • src/bun.js/api/glob.zig
  • src/glob/GlobWalker.zig

Walkthrough

Removed precomputed codepoint tracking from the Glob struct, refactored memory allocation in the constructor to use bun.new() with immutable pattern storage, and corrected a typo in a debug method name.

Changes

Cohort / File(s) Summary
Glob structure refactoring
src/bun.js/api/glob.zig
Removed pattern_codepoints field and associated precomputation logic, refactored constructor to use bun.new(Glob, ...) with immutable []const u8 pattern storage, updated memory management in finalize to use bun.default_allocator and bun.destroy(), and deleted convertUtf8 helper and CodepointIterator import.
Debug method naming fix
src/glob/GlobWalker.zig
Corrected typo in exported debug method: renamed debugPatternComopnents to debugPatternComponents and updated its call site.
🚥 Pre-merge checks | ✅ 1 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The description explains what the PR does (allocator fix and dead code removal) but lacks a section on verification of the code changes. Add a 'How did you verify your code works?' section explaining testing or validation steps performed.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main changes: fixing an allocator mismatch in the finalize function and removing dead code (pattern_codepoints field and convertUtf8 function).

✏️ 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.

@alii

alii commented Apr 16, 2026

Copy link
Copy Markdown
Member Author

@robobun adopt

@robobun

robobun commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator

✅ Adopted. Builds green on all platforms, no glob test failures.

Remaining CI red is pre-existing on main and unrelated to this change:

Ready to merge.

@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 — correct allocator pairing, clean dead-code removal, and trivial typo fix.

Extended reasoning...

Overview

Two files changed: src/bun.js/api/glob.zig (constructor/finalize logic) and src/glob/GlobWalker.zig (debug helper rename). The PR fixes an allocator mismatch, removes unreachable code, drops a redundant cast, and fixes a function-name typo.

Security risks

None. No auth, crypto, or permission-related code is touched. The allocator fix closes a latent memory-safety issue (use-after-free under heap_breakdown or alternative allocator configurations), making the code more correct, not less safe.

Level of scrutiny

Low. Each change is a mechanical fix with a clear 1:1 mapping between cause and remedy:

  • bun.new/bun.destroy (backed by bun.default_allocator) replaces the mismatched alloc.create/VirtualMachine.get().allocator.destroy pair.
  • bun.default_allocator.free(this.pattern) matches the allocator used by toSliceClone.
  • @constCast removal follows naturally from changing the slice type to []const u8.
  • pattern_codepoints field and convertUtf8 function had zero callers and are safely deleted.
  • The typo rename updates both the definition and its only call site.

Other factors

No existing tests are affected by the dead-code removal. The function rename (debugPatternComopnentsdebugPatternComponents) is guarded by bun.Environment.allow_assert so it only runs in debug builds. CI is already running on the commit.

@alii
alii merged commit 55ab304 into main Apr 17, 2026
61 of 67 checks passed
@alii
alii deleted the ali/glob-dead-code branch April 17, 2026 23:54
structwafel pushed a commit to structwafel/bun that referenced this pull request Apr 25, 2026
…h#29381)

The `Glob` struct and its pattern string were allocated with
`bun.default_allocator` but freed with `VirtualMachine.get().allocator`
(a mimalloc arena). This works today only because mimalloc's `mi_free`
ignores heap ownership, but is a contract violation that breaks under
`heap_breakdown` or future allocator changes.

- Use `bun.new`/`bun.destroy` for the `Glob` struct.
- Free pattern with `bun.default_allocator` to match `toSliceClone`.
- Drop the unnecessary `@constCast`.
- Remove dead `pattern_codepoints` field and `convertUtf8` function (no
callers anywhere in the tree).
- Fix `debugPatternComopnents` typo.
xhjkl pushed a commit to xhjkl/bun that referenced this pull request May 14, 2026
…h#29381)

The `Glob` struct and its pattern string were allocated with
`bun.default_allocator` but freed with `VirtualMachine.get().allocator`
(a mimalloc arena). This works today only because mimalloc's `mi_free`
ignores heap ownership, but is a contract violation that breaks under
`heap_breakdown` or future allocator changes.

- Use `bun.new`/`bun.destroy` for the `Glob` struct.
- Free pattern with `bun.default_allocator` to match `toSliceClone`.
- Drop the unnecessary `@constCast`.
- Remove dead `pattern_codepoints` field and `convertUtf8` function (no
callers anywhere in the tree).
- Fix `debugPatternComopnents` typo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants