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..b58b21a4f247 100644 --- a/test/cli/install/semver.test.ts +++ b/test/cli/install/semver.test.ts @@ -241,6 +241,25 @@ describe("Bun.semver.satisfies()", () => { testSatisfiesExact("5.0.0-beta.1", "5.0.0", false); }); + 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]) { + 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); + } + // 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", () => { testSatisfies("~1.2.3", "1.2.3", true); testSatisfies("~1.2", "1.2.0", true);