From a29853e6aa1a04b5a1dc2be79c652b377d4d5e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Mon, 27 Nov 2023 10:21:34 +0100 Subject: [PATCH] Don't crash on unexpected version string format (#358) --- esp-wifi/build.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/esp-wifi/build.rs b/esp-wifi/build.rs index eb52ab935f4..8933122af0a 100644 --- a/esp-wifi/build.rs +++ b/esp-wifi/build.rs @@ -86,6 +86,7 @@ fn main() -> Result<(), String> { // HACK: we detect the xtensa-enabled compiler by existence of the second version string in parens // - upstream output format: rustc 1.75.0-nightly (cae0791da 2023-10-05) // - xtensa output format: rustc 1.73.0-nightly (9163a2087 2023-10-03) (1.73.0.0) + // - gentoo format (non-xtensa): rustc 1.73.0-nightly (cc66ad468 2023-10-03) (gentoo) if version_string.chars().filter(|&c| c == '(').count() == 2 { let version = version_string .split('(') @@ -94,24 +95,27 @@ fn main() -> Result<(), String> { .split(')') .next() .unwrap(); - - let mut version = version.trim_start_matches('v').split('.'); - - let major = version.next().unwrap().parse::().unwrap(); - let minor = version.next().unwrap().parse::().unwrap(); - let patch = version.next().unwrap().parse::().unwrap(); - let release = version.next().unwrap().parse::().unwrap(); - - let version = Version4(major, minor, patch, release); - - if version >= Version4(1, 73, 0, 1) { - println!("cargo:rustc-cfg=xtensa_has_vaarg"); + if let Some(version) = try_read_xtensa_rustc_version(version) { + if version >= Version4(1, 73, 0, 1) { + println!("cargo:rustc-cfg=xtensa_has_vaarg"); + } } } Ok(()) } +fn try_read_xtensa_rustc_version(version: &str) -> Option { + let mut version = version.trim_start_matches('v').split('.'); + + let major = version.next()?.parse::().ok()?; + let minor = version.next()?.parse::().ok()?; + let patch = version.next()?.parse::().ok()?; + let release = version.next()?.parse::().ok()?; + + Some(Version4(major, minor, patch, release)) +} + #[cfg(not(any( feature = "esp32", feature = "esp32c2",