Skip to content

Commit

Permalink
platform-host: fix musl version detection bug
Browse files Browse the repository at this point in the history
I'm not quite sure how this was every working before. It's possible
it has never worked, but that our other methods of OS detection were
working.

In any case, empirically, it seems that the version number components
for musl can be a single digit.

Fixes #1427
  • Loading branch information
BurntSushi committed Feb 16, 2024
1 parent b9c76f4 commit 2645618
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions crates/platform-host/src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn detect_musl_version(ld_path: impl AsRef<Path>) -> Result<Os, PlatformError> {

fn musl_ld_output_to_version(kind: &str, output: &[u8]) -> Result<Os, PlatformError> {
static RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"Version ([0-9]{2,4})\.([0-9]{2,4})").unwrap());
Lazy::new(|| Regex::new(r"Version ([0-9]{1,4})\.([0-9]{1,4})").unwrap());

let output = std::str::from_utf8(output).map_err(|err| {
PlatformError::OsVersionDetectionError(format!("failed to parse {kind} as UTF-8: {err}"))
Expand All @@ -180,7 +180,7 @@ fn musl_ld_output_to_version(kind: &str, output: &[u8]) -> Result<Os, PlatformEr
RE.as_str(),
)));
};
// OK since we are guaranteed to have between 2 and 4 ASCII digits and the
// OK since we are guaranteed to have between 1 and 4 ASCII digits and the
// maximum possible value, 9999, fits into a u16.
let major = major.parse().expect("valid major version");
let minor = minor.parse().expect("valid minor version");
Expand Down Expand Up @@ -242,4 +242,21 @@ Written by Roland McGrath and Ulrich Drepper.",
}
);
}

#[test]
fn parse_musl_ld_output() {
// This output was generated by running `/lib/ld-musl-x86_64.so.1`
// in an Alpine Docker image. The Alpine version:
//
// # cat /etc/alpine-release
// 3.19.1
let output = b"\
musl libc (x86_64)
Version 1.2.4_git20230717
Dynamic Program Loader
Usage: /lib/ld-musl-x86_64.so.1 [options] [--] pathname [args]\
";
let got = musl_ld_output_to_version("stderr", output).unwrap();
assert_eq!(got, Os::Musllinux { major: 1, minor: 2 });
}
}

0 comments on commit 2645618

Please sign in to comment.