From ac5f0a93b2cbe39ec7d3fd026163d8d92584947c Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 18 Jul 2026 00:00:44 +0000 Subject: [PATCH 1/2] semver: parse_version_number calls parse_ascii directly on the already-all-digit slice All three call sites pass a slice bounded by the parser's own digit scan, so the byte-by-byte match on X/x/*/space/dot and the copy into a 20-byte stack buffer never do anything. The 20-byte cap also meant a 21+ digit component returned None (a wildcard) while a 20-digit overflow returned Some(0), so the two overflow lengths disagreed. Both now return Some(0). --- src/semver/Version.rs | 47 +++------------------------------ test/cli/install/semver.test.ts | 16 +++++++++++ 2 files changed, 20 insertions(+), 43 deletions(-) diff --git a/src/semver/Version.rs b/src/semver/Version.rs index 286705e86f84..4ab302f02375 100644 --- a/src/semver/Version.rs +++ b/src/semver/Version.rs @@ -664,49 +664,10 @@ impl VersionType { } fn parse_version_number(input: &[u8]) -> Option { - // max decimal u64 is 18446744073709551615 - let mut bytes = [0u8; 20]; - let mut byte_i: u8 = 0; - - debug_assert!(input[0] != b'.'); - - for &char in input { - match char { - b'X' | b'x' | b'*' => return None, - b'0'..=b'9' => { - // out of bounds - if (byte_i as usize) + 1 > bytes.len() { - return None; - } - bytes[byte_i as usize] = char; - byte_i += 1; - } - b' ' | b'.' => break, - // ignore invalid characters - _ => {} - } - } - - // If there are no numbers - if byte_i == 0 { - return None; - } - - if bun_core::env::IS_DEBUG { - return match T::parse_ascii(&bytes[0..byte_i as usize]) { - Some(v) => Some(v), - None => { - bun_core::pretty_errorln!( - "ERROR parsing version: \"{}\", bytes: {}", - bstr::BStr::new(input), - bstr::BStr::new(&bytes[0..byte_i as usize]), - ); - Some(T::ZERO) - } - }; - } - - Some(T::parse_ascii(&bytes[0..byte_i as usize]).unwrap_or(T::ZERO)) + // Callers slice `input` from the first digit to the first non-digit, + // so every byte is already `b'0'..=b'9'` and the slice is non-empty. + debug_assert!(!input.is_empty() && input.iter().all(u8::is_ascii_digit)); + Some(T::parse_ascii(input).unwrap_or(T::ZERO)) } } diff --git a/test/cli/install/semver.test.ts b/test/cli/install/semver.test.ts index 266117ab69ca..be5dade5b8cb 100644 --- a/test/cli/install/semver.test.ts +++ b/test/cli/install/semver.test.ts @@ -241,6 +241,22 @@ describe("Bun.semver.satisfies()", () => { testSatisfiesExact("5.0.0-beta.1", "5.0.0", false); }); + test("overflow-length version components are not treated as wildcards", () => { + // A major/minor/patch component longer than 20 digits must behave the same + // as one that is exactly 20 digits and overflows u64. Previously the 21+ + // digit case fell through to `None` (a wildcard) so any version matched. + // node-semver returns false for all of these. + const twenty = Buffer.alloc(20, "9").toString(); + const twentyOne = Buffer.alloc(21, "9").toString(); + for (const big of [twenty, twentyOne]) { + testSatisfies("^" + big, "1.0.0", false); + testSatisfies("^" + big, "5.0.0", false); + testSatisfies("~" + big, "1.0.0", false); + testSatisfies("~" + big, "5.0.0", false); + testSatisfies("~1." + big, "2.0.0", false); + } + }); + test("ranges", () => { testSatisfies("~1.2.3", "1.2.3", true); testSatisfies("~1.2", "1.2.0", true); From 1994a775074cd77c25e3e11bb7a7b756e3bcf341 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 18 Jul 2026 00:34:29 +0000 Subject: [PATCH 2/2] semver: cover the non-overflow >20-byte path and trim the test comment A component like thirty leading zeros followed by 5 is 31 bytes but has value 5, so parse_ascii returns Ok(5) rather than hitting the overflow fallback. The new assertions distinguish Some(5) from both the old wildcard and from Some(0). --- test/cli/install/semver.test.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/cli/install/semver.test.ts b/test/cli/install/semver.test.ts index be5dade5b8cb..b58b21a4f247 100644 --- a/test/cli/install/semver.test.ts +++ b/test/cli/install/semver.test.ts @@ -241,11 +241,9 @@ describe("Bun.semver.satisfies()", () => { testSatisfiesExact("5.0.0-beta.1", "5.0.0", false); }); - test("overflow-length version components are not treated as wildcards", () => { - // A major/minor/patch component longer than 20 digits must behave the same - // as one that is exactly 20 digits and overflows u64. Previously the 21+ - // digit case fell through to `None` (a wildcard) so any version matched. - // node-semver returns false for all of these. + test("long version components are not treated as wildcards", () => { + // A component >20 bytes must parse to its numeric value (or clamp on overflow), + // not fall through to a wildcard. node-semver loose mode agrees with all of these. const twenty = Buffer.alloc(20, "9").toString(); const twentyOne = Buffer.alloc(21, "9").toString(); for (const big of [twenty, twentyOne]) { @@ -255,6 +253,11 @@ describe("Bun.semver.satisfies()", () => { testSatisfies("~" + big, "5.0.0", false); testSatisfies("~1." + big, "2.0.0", false); } + // 31 bytes but value 5: must be Some(5), not wildcard and not Some(0). + const padded = Buffer.alloc(30, "0").toString() + "5"; + testSatisfies("^" + padded, "5.2.0", true); + testSatisfies("^" + padded, "0.5.0", false); + testSatisfies("^" + padded, "6.0.0", false); }); test("ranges", () => {