From abc5cfb9508c4d139b39bff335c4aa6a288a7886 Mon Sep 17 00:00:00 2001 From: Bart Massey Date: Tue, 14 Feb 2023 00:21:16 -0800 Subject: [PATCH] coreutils: fixed panic when multi-call binary has empty or non-UTF-8 name The multi-call `coreutils` binary starts by trying to convert its invocation path into a UTF-8 string, panicking if this doesn't work. This patch makes `coreutils` exit gracefully in this case. --- src/bin/coreutils.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 08c0774ba..75e64e17b 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -41,8 +41,8 @@ fn binary_path(args: &mut impl Iterator) -> PathBuf { } } -fn name(binary_path: &Path) -> &str { - binary_path.file_stem().unwrap().to_str().unwrap() +fn name(binary_path: &Path) -> Option<&str> { + binary_path.file_stem()?.to_str() } fn main() { @@ -52,7 +52,10 @@ fn main() { let mut args = uucore::args_os(); let binary = binary_path(&mut args); - let binary_as_util = name(&binary); + let binary_as_util = name(&binary).unwrap_or_else(|| { + usage(&utils, ""); + process::exit(0); + }); // binary name equals util name? if let Some(&(uumain, _)) = utils.get(binary_as_util) {