Repository navigation
semver: clamp desugared exclusive upper bound at u64::MAX instead of saturating to empty range - #34600
Conversation
…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.
|
Status: ready for review. Reproduced with CI: |
|
Warning Review limit reached
Next review available in: 8 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
Comment |
|
Updated 4:26 AM PT - Jul 18th, 2026
✅ @robobun, your commit bc625945191d0ed6a163801450e7008d6a59a817 passed in 🧪 To try this PR locally: bunx bun-pr 34600That installs a local version of the PR into your bun-34600 --bun |
There was a problem hiding this comment.
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 theSomebranch of each new helper produces the exact sameComparatorvalue as the old mutation-in-place code. - Confirmed no
saturating_addremains insrc/semver/. - Checked that the hyphen-range
Somebranches still mutatesecond_versionin place (preserving itstag) rather than routing through the tag-dropping helper — only the overflow fallback uses the fresh-Versionpath. - 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.
There was a problem hiding this comment.
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 sameComparatoras the oldsaturating_addcode (Version::default()zeros unmentioned fields); overflow arm switches< X→<= ceiling. - Confirmed no
saturating_addremains insrc/semver/— all ten desugar sites covered. - Checked the hyphen-range
Some(m)arm still mutatessecond_versionin place, so the non-overflow comparator is unchanged; the redundantchecked_addbefore callinglt_next_*onNoneis 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.
Reproduction
Cause
Caret, tilde, x-range, bare-partial, and hyphen-partial range forms all desugar to
>=X <{component+1}. The ten desugar sites inSemverQuery.rs/SemverRange.rscomputed the exclusive upper bound withcomponent.saturating_add(1). Atu64::MAX, saturation leaves the component unchanged, so the right comparator becomes<MAX.0.0while the left is>=MAX.0.0: an empty range. A version atu64::MAXwould satisfy*and=vbut fail every partial-derived range that names it.Fix
Add
Comparator::lt_next_major/lt_next_minor/lt_next_patchwhich usechecked_add(1)and, on overflow, return<= {ceiling}(e.g.<= MAX.MAX.MAXfor the major case) instead of a saturated<. Replace all tensaturating_add(1)sites. The non-overflow path produces byte-identicalComparatorvalues, so this is a pure edge-case fix with no change to ordinary ranges.Verification
[review] gate passed · iteration 1 · 3 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 1 passed · 1 rejected · iteration 1
evidence per changed file