Skip to content

semver: clamp desugared exclusive upper bound at u64::MAX instead of saturating to empty range - #34600

Merged
Jarred-Sumner merged 3 commits into
mainfrom
claude/farm/6a8dbbf0/semver-u64-max-saturation
Jul 18, 2026
Merged

Jarred-Sumner merged 3 commits into
mainfrom
claude/farm/6a8dbbf0/semver-u64-max-saturation

Conversation

@robobun

@robobun robobun commented Jul 18, 2026 •

Copy link
Copy Markdown
Collaborator

Reproduction

const M = "18446744073709551615"; // u64::MAX
Bun.semver.satisfies(`${M}.0.0`, `^${M}`);       // false, should be true
Bun.semver.satisfies(`${M}.0.0`, `~${M}`);       // false, should be true
Bun.semver.satisfies(`${M}.0.0`, M);             // false, should be true
Bun.semver.satisfies(`${M}.0.0`, `${M}.x`);      // false, should be true
Bun.semver.satisfies(`1.${M}.3`, `~1.${M}.3`);   // false, should be true
Bun.semver.satisfies(`0.0.${M}`, `^0.0.${M}`);   // false, should be true
Bun.semver.satisfies(`${M}.5.0`, `1.0.0 - ${M}`); // false, should be true
// control: MAX-1 works
Bun.semver.satisfies("18446744073709551614.0.0", "^18446744073709551614"); // true

Cause

Caret, tilde, x-range, bare-partial, and hyphen-partial range forms all desugar to >=X <{component+1}. The ten desugar sites in SemverQuery.rs / SemverRange.rs computed the exclusive upper bound with component.saturating_add(1). At u64::MAX, saturation leaves the component unchanged, so the right comparator becomes <MAX.0.0 while the left is >=MAX.0.0: an empty range. A version at u64::MAX would satisfy * and =v but fail every partial-derived range that names it.

Fix

Add Comparator::lt_next_major / lt_next_minor / lt_next_patch which use checked_add(1) and, on overflow, return <= {ceiling} (e.g. <= MAX.MAX.MAX for the major case) instead of a saturated <. Replace all ten saturating_add(1) sites. The non-overflow path produces byte-identical Comparator values, so this is a pure edge-case fix with no change to ordinary ranges.

Verification

$ USE_SYSTEM_BUN=1 bun test test/cli/install/semver.test.ts -t "u64::MAX"
(fail) Bun.semver.satisfies() > u64::MAX component does not collapse ^/~/x/hyphen ranges to empty

$ bun bd test test/cli/install/semver.test.ts
 28 pass
 0 fail

[review] gate passed · iteration 1 · 3 files touched

fails on main (without fix)
ASAN without fix: 1 FAILED
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/cli/install/semver.test.ts
info: syncing channel updates for nightly-2026-05-06-x86_64-unknown-linux-gnu
info: latest update on 2026-05-06 for version 1.97.0-nightly (e95e73209 2026-05-05)
info: component rust-src is up to date
info: checking for self-update (current version: 1.29.0)
bun test v1.4.0 (bc6259451)

test/cli/install/semver.test.ts:
(pass) Bun.semver.order() > whitespace bug fix [5.58ms]
(pass) Bun.semver.order() > comparisons [58.20ms]
(pass) Bun.semver.order() > loose compare("0", "0.0") [4.15ms]
(pass) Bun.semver.order() > loose compare("1", "1.0") [2.29ms]
(pass) Bun.semver.order() > loose compare("1.2", "1.2.0") [1.07ms]
(pass) Bun.semver.order() > loose compare("1.x", "1.0.x") [1.06ms]
(pass) Bun.semver.order() > loose compare("1.x.x", "1.0.x") [1.14ms]
(pass) Bun.semver.order() > loose compare("2.x", "1.x") [1.18ms]
(pass) Bun.semver.order() > loose compare("2.x", "2.1") [1.12ms]
(pass) Bun.semver.order() > loose compare("2", "1") [0.93ms]
(pass) Bun.semver.order() > loose compare("3.*", "3.1") [0.82ms]
(pass) B
... (truncated)

release without fix: all passed
bun test v1.4.0-canary.1 (bc6259451)

test/cli/install/semver.test.ts:
(pass) Bun.semver.order() > whitespace bug fix [0.07ms]
(pass) Bun.semver.order() > comparisons [0.28ms]
(pass) Bun.semver.order() > loose compare("0", "0.0") [0.05ms]
(pass) Bun.semver.order() > loose compare("1", "1.0")
(pass) Bun.semver.order() > loose compare("1.2", "1.2.0")
(pass) Bun.semver.order() > loose compare("1.x", "1.0.x")
(pass) Bun.semver.order() > loose compare("1.x.x", "1.0.x")
(pass) Bun.semver.order() > loose compare("2.x", "1.x")
(pass) Bun.semver.order() > loose compare("2.x", "2.1")
(pass) Bun.semver.order() > loose compare("2", "1")
(pass) Bun.semver.order() > loose compare("3.*", "3.1")
(pass) Bun.semver.order() > loose compare("3.2.*", "3.2.0")
(pass) Bun.semver.order() > loose compare("4294967295.4294967295.x", "4294967295.4294967295.4294967294")
(pass) Bun.semver.order() > loose compare("*", "4294967295.4294967295.4294967294") [0.02ms]
(pass) Bun.semver.order() > equality [0.26ms]
(pass) Bun.semver.satisfies() > expected errors [0.22ms]
(pass) Bun.semver.satisfies() > failures does not cause weird memory issues [90.46ms]
(pass) Bun.semver.satisfies() > exact versions [0
... (truncated)
passes on PR (with fix)
ASAN with fix: all passed
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/cli/install/semver.test.ts
info: syncing channel updates for nightly-2026-05-06-x86_64-unknown-linux-gnu
info: latest update on 2026-05-06 for version 1.97.0-nightly (e95e73209 2026-05-05)
info: component rust-src is up to date
info: checking for self-update (current version: 1.29.0)
bun test v1.4.0 (bc6259451)

test/cli/install/semver.test.ts:
(pass) Bun.semver.order() > whitespace bug fix [4.61ms]
(pass) Bun.semver.order() > comparisons [40.00ms]
(pass) Bun.semver.order() > loose compare("0", "0.0") [3.74ms]
(pass) Bun.semver.order() > loose compare("1", "1.0") [1.81ms]
(pass) Bun.semver.order() > loose compare("1.2", "1.2.0") [1.05ms]
(pass) Bun.semver.order() > loose compare("1.x", "1.0.x") [1.51ms]
(pass) Bun.semver.order() > loose compare("1.x.x", "1.0.x") [0.90ms]
(pass) Bun.semver.order() > loose compare("2.x", "1.x") [0.91ms]
(pass) Bun.semver.order() > loose compare("2.x", "2.1") [0.92ms]
(pass) Bun.semver.order() > loose compare("2", "1") [0.97ms]
(pass) Bun.semver.order() > loose compare("3.*", "3.1") [1.00ms]
(pass) B
... (truncated)

release with fix: all passed
$ bun scripts/build.ts --profile=release
info: syncing channel updates for nightly-2026-05-06-x86_64-unknown-linux-gnu
info: latest update on 2026-05-06 for version 1.97.0-nightly (e95e73209 2026-05-05)
info: component rust-src is up to date
info: checking for self-update (current version: 1.29.0)
[configured] bun-profile → bun (stripped) in 1451ms (unchanged)
ninja: Entering directory `/workspace/bun/build/release'
[0/4] cargo bun_bin → libbun_rust.a (--target x86_64-unknown-linux-gnu)
info: syncing channel updates for nightly-2026-05-06-x86_64-unknown-linux-gnu
info: latest update on 2026-05-06 for version 1.97.0-nightly (e95e73209 2026-05-05)
info: component rust-src is up to date
info: component rust-std is up to date

  nightly-2026-05-06-x86_64-unknown-linux-gnu unchanged - rustc 1.97.0-nightly (e95e73209 2026-05-05)

info: checking for self-update (current version: 1.29.0)
�[1m�[92m   Compiling�[0m bun_semver v0.0.0 (/workspace/bun/src/semver)
�[1m�[92m   Compiling�[0m bun_analytics v0.0.0 (/workspace/bun/src/analytics)
�[1m�[92m   Compiling�[0m bun_install_types v0.0.0 (/workspace/bun/src/install_types)
�[1m�[92m   Compiling�[0m bun_spawn_sys v0.0.0 (/wo
... (truncated)
diff hotspot
src/semver/SemverQuery.rs       |  66 +++++++++++---------
 src/semver/SemverRange.rs       | 135 ++++++++++++++++++++++++++--------------
 test/cli/install/semver.test.ts |  52 ++++++++++++++++
 3 files changed, 178 insertions(+), 75 deletions(-)

gate history · 1 passed · 1 rejected · iteration 1

evidence per changed file
file                             reads  edits  tests
src/semver/SemverQuery.rs            1      3      0
src/semver/SemverRange.rs            1      2      0
test/cli/install/semver.test.ts      2      1      0

…saturating to empty range

Caret/tilde/x-range/bare-partial/hyphen-partial desugar to `>=X <{X+1}`.
When the incremented component is u64::MAX, `saturating_add(1)` left it at
MAX, yielding `>=MAX <MAX` (the empty range), so a version would fail to
satisfy its own `^v`/`~v` and a hyphen range naming MAX excluded the entire
MAX.* line it names.

Replace the ten `saturating_add(1)` sites with `checked_add` via three new
`Comparator::lt_next_{major,minor,patch}` helpers that fall back to
`<= MAX.MAX.MAX` (or the appropriate component ceiling) on overflow.
Non-overflow paths produce byte-identical comparators.
@robobun

robobun commented Jul 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: ready for review.

Reproduced with USE_SYSTEM_BUN=1 bun test test/cli/install/semver.test.ts -t "u64::MAX" (fails), passes with bun bd test test/cli/install/semver.test.ts (28 pass, 0 fail). Gate confirms: test fails without the fix and passes with it on both ASAN and release builds.

CI: semver.test.ts passes on every lane. The remaining reds on build #75284 are unrelated flakes that passed on retry (spawn timeout on Windows, next-pages dev-server ConnectionRefused, install hoisting/lifecycle/security-scanner on Windows/Alpine, serve-body-leak on macOS) plus one darwin-14 lane that expired. None touch semver.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 8 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6b9dc313-4f56-4e93-a9e1-b6d23ddd3643

📥 Commits

Reviewing files that changed from the base of the PR and between 511caaa and bc62594.

📒 Files selected for processing (3)
  • src/semver/SemverQuery.rs
  • src/semver/SemverRange.rs
  • test/cli/install/semver.test.ts

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

@robobun

robobun commented Jul 18, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 4:26 AM PT - Jul 18th, 2026

✅ @robobun, your commit bc625945191d0ed6a163801450e7008d6a59a817 passed in Build #75284! 🎉


🧪   To try this PR locally:

bunx bun-pr 34600

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

bun-34600 --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 — focused edge-case fix; non-overflow path is byte-identical to before.

What was reviewed:

  • Traced each of the ten replaced saturating_add(1) sites (caret, tilde, init_wildcard, hyphen Minor/Patch) and confirmed the Some branch of each new helper produces the exact same Comparator value as the old mutation-in-place code.
  • Confirmed no saturating_add remains in src/semver/.
  • Checked that the hyphen-range Some branches still mutate second_version in place (preserving its tag) rather than routing through the tag-dropping helper — only the overflow fallback uses the fresh-Version path.
  • Test covers every modified desugar shape plus negative (ceiling still enforced) and MAX-1 control cases.
Extended reasoning...

Overview

Replaces ten component.saturating_add(1) sites across SemverQuery.rs and SemverRange.rs with three new Comparator::lt_next_{major,minor,patch} helpers that use checked_add(1) and fall back to an inclusive <= ceiling on overflow. Adds a test block in test/cli/install/semver.test.ts exercising ^, ~, bare-partial, x-range, and hyphen-partial forms at u64::MAX.

Correctness

I walked each call site and compared the old mutate-then-fill pattern against the new whole-Comparator assignment. In every non-overflow case the resulting {op, version} is identical: lt_next_major(5) yields Lt {6,0,0,default_tag}, exactly what the old range.right.version.major = 5.saturating_add(1) produced on a default-initialized Lt comparator. The caret branch's early range.right = Lt assignment was safely folded into the per-exit assignments — the major == None guard still leaves range.right at its default Unset. The hyphen-range branches keep the outer checked_add so the common path continues to carry second_version (including any tag) verbatim, and only the None arm delegates to the helper. Grep confirms no saturating_add remains under src/semver/.

Security risks

None. Pure arithmetic on already-parsed u64 version components; no new input surface, allocation, or unsafe code.

Level of scrutiny

Low-to-medium. The fix is at the right layer (shared helpers on Comparator), covers the whole bug class in one PR, and the claim that ordinary ranges are unaffected is verifiable by inspection — every Some(m) arm constructs the same struct the old code did. The overflow fallback semantics (<= {ceiling}) are the natural interpretation and match how Wildcard::Major already used Lte MAX.MAX.MAX elsewhere in the file.

Other factors

Test is well-structured: sanity assertions that MAX.0.0 is a valid version, one positive case per desugar path, negative cases proving the clamped upper bound doesn't leak past its ceiling, and a MAX-1 control block. Verified locally against system bun (fails) and debug build (passes). No prior reviewer comments to address.

@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 — clean edge-case fix with byte-identical behavior on the non-overflow path.

What was reviewed:

  • Traced all three lt_next_* helpers: non-overflow arm produces the same Comparator as the old saturating_add code (Version::default() zeros unmentioned fields); overflow arm switches < X → <= ceiling.
  • Confirmed no saturating_add remains in src/semver/ — all ten desugar sites covered.
  • Checked the hyphen-range Some(m) arm still mutates second_version in place, so the non-overflow comparator is unchanged; the redundant checked_add before calling lt_next_* on None is harmless.
  • Test covers every changed range shape plus negative assertions that the clamped upper bound still excludes the next major/minor, and a MAX-1 control.
Extended reasoning...

Overview

Replaces ten component.saturating_add(1) sites in semver range desugaring (caret, tilde, x-range/bare-partial via init_wildcard, and hyphen-range partial endpoints) with three new Comparator::lt_next_{major,minor,patch} helpers. Each helper does checked_add(1): on Some, it builds the same {op: Lt, version: {..+1, ..default}} comparator the old code produced; on None (component == u64::MAX), it returns {op: Lte, version: ceiling} so >=MAX <MAX becomes >=MAX <=MAX.MAX.MAX instead of an empty range. Adds a 50-line test block exercising every shape at u64::MAX and u64::MAX-1.

Security risks

None. Pure arithmetic in the semver comparator constructor; no untrusted-length allocation, no I/O, no unsafe. The only behavioral change is at the u64::MAX boundary, where the previous code already accepted the input and simply produced an unsatisfiable range.

Level of scrutiny

Medium. The semver parser feeds bun install resolution, so a regression on ordinary ranges would be high-impact — but the non-overflow path is mechanically identical: I verified Version::default() zeros major/minor/patch/tag, so e.g. lt_next_minor(major, minor) on non-overflow yields exactly what the old {op: Lt, ..default} + field mutation produced. The overflow path is unreachable for any real npm version and only affects synthetic u64::MAX inputs. A grep confirms no saturating_add remains in src/semver/, so the whole class is fixed.

Other factors

The test is thorough per REVIEW.md's variant-matrix guidance: it hits caret at all three depths, tilde at both depths, bare-partial and .x at both depths, hyphen with both partial-minor and partial-patch endpoints, asserts the clamped ceiling still rejects the next major/minor (so <= didn't leak), and includes a MAX-1 control row proving the shapes already worked one below the boundary. The PR body reports USE_SYSTEM_BUN=1 fails and bun bd test passes (28/0). The one stylistic wrinkle — hyphen desugar doing its own checked_add before falling back to lt_next_* on overflow — was raised and ruled out by the finder pass; it's redundant but preserves exact non-overflow bytes and is not a bug.

@Jarred-Sumner
Jarred-Sumner merged commit bfd8e11 into main Jul 18, 2026
79 checks passed
@Jarred-Sumner
Jarred-Sumner deleted the claude/farm/6a8dbbf0/semver-u64-max-saturation branch July 18, 2026 23:05
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.

2 participants