From 24d7811fc0f49e1bde7e00922d0c63c8423ecec1 Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Sun, 29 Nov 2026 15:58:14 +0300 Subject: [PATCH 1/4] coreutils: resolve utility from symlink target before prefix matching (#16848) --- src/bin/coreutils.rs | 52 +++++++++++++++++++++++++++++++++-------- tests/test_util_name.rs | 27 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 3235fefd44a..fdc1b980df9 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -20,10 +20,24 @@ fn matched_utility(name: &str, utils: &UtilityMap) -> Option<&'static str> if name.ends_with("utils") { return None; } - // Prefer stty over tty, and never treat *utils as ls. + // Exact match has highest precedence. + if utils.contains_key(name) { + return utils.keys().find(|&&k| k == name).copied(); + } + // Prefix match: e.g. "uu-cat", "coreutils-cat", "x86_64-linux-gnu-cat", "gcat". + // Must be separated by a delimiter ('-', '_', '.') or the standard 'g' (GNU) prefix. utils .keys() - .filter(|&&util| name.ends_with(util)) + .filter(|&&util| { + if let Some(prefix) = name.strip_suffix(util) { + prefix.ends_with('-') + || prefix.ends_with('_') + || prefix.ends_with('.') + || prefix == "g" + } else { + false + } + }) .max_by_key(|util| util.len()) .copied() } @@ -32,9 +46,29 @@ fn utility_from_symlink( binary: &std::path::Path, utils: &UtilityMap, ) -> Option<&'static str> { - let target = std::fs::read_link(binary).ok()?; - let name = validation::name(&target)?; - matched_utility(name, utils) + let mut current = binary.to_path_buf(); + for _ in 0..40 { + let target = std::fs::read_link(¤t).ok()?; + let target = if target.is_relative() { + if let Some(parent) = current.parent() { + parent.join(target) + } else { + target + } + } else { + target + }; + if let Some(name) = validation::name(&target) { + if let Some(util) = matched_utility(name, utils) { + return Some(util); + } + if name.ends_with("utils") || name.ends_with("box") { + break; + } + } + current = target; + } + None } fn usage(utils: &UtilityMap, name: &str) { @@ -75,7 +109,9 @@ fn main() -> ExitCode { // binary name ends with util name? let is_coreutils = binary_as_util.ends_with("utils"); - let util_name = if let Some(util) = matched_utility(binary_as_util, &utils) { + let util_name = if let Some(util) = utility_from_symlink(&binary, &utils) { + Some(OsString::from(util)) + } else if let Some(util) = matched_utility(binary_as_util, &utils) { Some(OsString::from(util)) } else if is_coreutils && let Some(util) = args @@ -95,10 +131,6 @@ fn main() -> ExitCode { // todo: Remove support of "*box" from binary uucore::set_utility_is_second_arg(); args.next() - } else if cfg!(all(target_os = "linux", target_env = "gnu")) - && let Some(util) = utility_from_symlink(&binary, &utils) - { - Some(OsString::from(util)) } else { return validation::not_found(&OsString::from(binary_as_util)); }; diff --git a/tests/test_util_name.rs b/tests/test_util_name.rs index 114feae15d9..d7689b1652d 100644 --- a/tests/test_util_name.rs +++ b/tests/test_util_name.rs @@ -486,3 +486,30 @@ fn coreutils_prog_diagnostics_use_selected_name() { assert!(!output.status.success()); assert!(String::from_utf8_lossy(&output.stderr).starts_with("ls:")); } + +#[test] +#[cfg(all(unix, feature = "pwd"))] +fn test_symlink_named_pwd_symlink() { + use std::process::Command; + + let scene = TestScenario::new("pwd_symlink"); + let at = &scene.fixtures; + at.mkdir("bin"); + at.mkdir("sub"); + + symlink_file(&scene.bin_path, at.plus("bin/pwd")).unwrap(); + symlink_file(at.plus("bin/pwd"), at.plus("sub/pwd-symlink")).unwrap(); + + let output = Command::new(at.plus("sub/pwd-symlink")).output().unwrap(); + + assert!( + output.status.success(), + "Expected pwd to succeed, got: {output:?}" + ); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(!stdout.is_empty()); + assert!( + output.stderr.is_empty(), + "Expected no error, got: {output:?}" + ); +} From 7a7633a587b162633b6777488a0c2ecc8eadd479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Arda=20G=C3=BCney?= <64425746+yunusarda-sudo@users.noreply.github.com> Date: Sun, 29 Nov 2026 17:25:43 +0300 Subject: [PATCH 2/4] coreutils: restore linux-gnu symlink fallback and simplify the test Keep restricted prefix matching as the #16848 fix, but call utility_from_symlink only on linux-gnu after name matching fails so usual invocations do not pay readlink. Drop the multi-hop walker (one hop is enough) and switch the regression test to true. --- src/bin/coreutils.rs | 34 ++++++++-------------------------- tests/test_util_name.rs | 20 +++++++++++--------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index fdc1b980df9..39d473ee46e 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -46,29 +46,9 @@ fn utility_from_symlink( binary: &std::path::Path, utils: &UtilityMap, ) -> Option<&'static str> { - let mut current = binary.to_path_buf(); - for _ in 0..40 { - let target = std::fs::read_link(¤t).ok()?; - let target = if target.is_relative() { - if let Some(parent) = current.parent() { - parent.join(target) - } else { - target - } - } else { - target - }; - if let Some(name) = validation::name(&target) { - if let Some(util) = matched_utility(name, utils) { - return Some(util); - } - if name.ends_with("utils") || name.ends_with("box") { - break; - } - } - current = target; - } - None + let target = std::fs::read_link(binary).ok()?; + let name = validation::name(&target)?; + matched_utility(name, utils) } fn usage(utils: &UtilityMap, name: &str) { @@ -109,9 +89,7 @@ fn main() -> ExitCode { // binary name ends with util name? let is_coreutils = binary_as_util.ends_with("utils"); - let util_name = if let Some(util) = utility_from_symlink(&binary, &utils) { - Some(OsString::from(util)) - } else if let Some(util) = matched_utility(binary_as_util, &utils) { + let util_name = if let Some(util) = matched_utility(binary_as_util, &utils) { Some(OsString::from(util)) } else if is_coreutils && let Some(util) = args @@ -131,6 +109,10 @@ fn main() -> ExitCode { // todo: Remove support of "*box" from binary uucore::set_utility_is_second_arg(); args.next() + } else if cfg!(all(target_os = "linux", target_env = "gnu")) + && let Some(util) = utility_from_symlink(&binary, &utils) + { + Some(OsString::from(util)) } else { return validation::not_found(&OsString::from(binary_as_util)); }; diff --git a/tests/test_util_name.rs b/tests/test_util_name.rs index d7689b1652d..b2813c712ee 100644 --- a/tests/test_util_name.rs +++ b/tests/test_util_name.rs @@ -488,26 +488,28 @@ fn coreutils_prog_diagnostics_use_selected_name() { } #[test] -#[cfg(all(unix, feature = "pwd"))] -fn test_symlink_named_pwd_symlink() { +#[cfg(all(target_os = "linux", target_env = "gnu", feature = "true"))] +fn test_symlink_named_true_symlink() { use std::process::Command; - let scene = TestScenario::new("pwd_symlink"); + let scene = TestScenario::new("true_symlink"); let at = &scene.fixtures; at.mkdir("bin"); at.mkdir("sub"); - symlink_file(&scene.bin_path, at.plus("bin/pwd")).unwrap(); - symlink_file(at.plus("bin/pwd"), at.plus("sub/pwd-symlink")).unwrap(); + symlink_file(&scene.bin_path, at.plus("bin/true")).unwrap(); + symlink_file(at.plus("bin/true"), at.plus("sub/true-symlink")).unwrap(); - let output = Command::new(at.plus("sub/pwd-symlink")).output().unwrap(); + let output = Command::new(at.plus("sub/true-symlink")).output().unwrap(); assert!( output.status.success(), - "Expected pwd to succeed, got: {output:?}" + "Expected true to succeed, got: {output:?}" + ); + assert!( + output.stdout.is_empty(), + "Expected no output, got: {output:?}" ); - let stdout = String::from_utf8_lossy(&output.stdout); - assert!(!stdout.is_empty()); assert!( output.stderr.is_empty(), "Expected no error, got: {output:?}" From 4d5b7923f4949a083f668e193b7efdf2bbd50475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Arda=20G=C3=BCney?= <64425746+yunusarda-sudo@users.noreply.github.com> Date: Mon, 30 Nov 2026 08:01:43 +0300 Subject: [PATCH 3/4] coreutils: allow single-letter PROG_PREFIX without a separator Fold exact matches into the prefix filter and keep uls/ucat working. --- src/bin/coreutils.rs | 22 ++++++++-------------- tests/test_util_name.rs | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 39d473ee46e..7945883aa04 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -20,23 +20,17 @@ fn matched_utility(name: &str, utils: &UtilityMap) -> Option<&'static str> if name.ends_with("utils") { return None; } - // Exact match has highest precedence. - if utils.contains_key(name) { - return utils.keys().find(|&&k| k == name).copied(); - } - // Prefix match: e.g. "uu-cat", "coreutils-cat", "x86_64-linux-gnu-cat", "gcat". - // Must be separated by a delimiter ('-', '_', '.') or the standard 'g' (GNU) prefix. + // Exact match, delimited prefix (uu-cat, coreutils-cat, x86_64-linux-gnu-cat), + // or a single-letter PROG_PREFIX without a separator (uls, ucat, gcat). + // Rejects false suffix matches such as pwd-symlink -> link. utils .keys() .filter(|&&util| { - if let Some(prefix) = name.strip_suffix(util) { - prefix.ends_with('-') - || prefix.ends_with('_') - || prefix.ends_with('.') - || prefix == "g" - } else { - false - } + name.strip_suffix(util).is_some_and(|prefix| { + prefix.is_empty() + || prefix.ends_with(['-', '_', '.']) + || matches!(prefix.as_bytes(), [b] if b.is_ascii_alphanumeric()) + }) }) .max_by_key(|util| util.len()) .copied() diff --git a/tests/test_util_name.rs b/tests/test_util_name.rs index b2813c712ee..27d94186f23 100644 --- a/tests/test_util_name.rs +++ b/tests/test_util_name.rs @@ -487,6 +487,23 @@ fn coreutils_prog_diagnostics_use_selected_name() { assert!(String::from_utf8_lossy(&output.stderr).starts_with("ls:")); } +#[test] +#[cfg(all(unix, feature = "ls"))] +fn test_undelimited_prog_prefix() { + use std::process::Command; + + let scene = TestScenario::new("uls"); + let uls = scene.fixtures.plus("uls"); + symlink_file(&scene.bin_path, &uls).unwrap(); + + let output = Command::new(&uls).arg("--help").output().unwrap(); + assert!(output.status.success(), "{output:?}"); + assert!( + String::from_utf8_lossy(&output.stdout).contains("Usage: ls"), + "{output:?}" + ); +} + #[test] #[cfg(all(target_os = "linux", target_env = "gnu", feature = "true"))] fn test_symlink_named_true_symlink() { From 87092c9b76bf8fa16fab848a7ebd35f4c43214f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20Arda=20G=C3=BCney?= <64425746+yunusarda-sudo@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:20:31 +0300 Subject: [PATCH 4/4] coreutils: drop symlink-target resolution from the prefix-matching fix Leave hardlink/symlink resolution to #16981 and keep a regression test that true-symlink does not false-match link. --- tests/test_util_name.rs | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/tests/test_util_name.rs b/tests/test_util_name.rs index 27d94186f23..da884842c39 100644 --- a/tests/test_util_name.rs +++ b/tests/test_util_name.rs @@ -505,30 +505,19 @@ fn test_undelimited_prog_prefix() { } #[test] -#[cfg(all(target_os = "linux", target_env = "gnu", feature = "true"))] -fn test_symlink_named_true_symlink() { +#[cfg(all(unix, feature = "link"))] +fn test_delimited_name_does_not_false_match_link() { use std::process::Command; let scene = TestScenario::new("true_symlink"); - let at = &scene.fixtures; - at.mkdir("bin"); - at.mkdir("sub"); - - symlink_file(&scene.bin_path, at.plus("bin/true")).unwrap(); - symlink_file(at.plus("bin/true"), at.plus("sub/true-symlink")).unwrap(); - - let output = Command::new(at.plus("sub/true-symlink")).output().unwrap(); + let alias = scene.fixtures.plus("true-symlink"); + symlink_file(&scene.bin_path, &alias).unwrap(); - assert!( - output.status.success(), - "Expected true to succeed, got: {output:?}" - ); - assert!( - output.stdout.is_empty(), - "Expected no output, got: {output:?}" - ); - assert!( - output.stderr.is_empty(), - "Expected no error, got: {output:?}" + let output = Command::new(&alias).output().unwrap(); + assert_eq!(output.status.code(), Some(1), "{output:?}"); + assert!(output.stdout.is_empty(), "{output:?}"); + assert_eq!( + output.stderr, b"coreutils: unknown program 'true-symlink'\n", + "{output:?}" ); }