Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 4 additions & 43 deletions src/semver/Version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,49 +664,10 @@ impl<T: VersionInt> VersionType<T> {
}

fn parse_version_number(input: &[u8]) -> Option<T> {
// 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))
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/cli/install/semver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading