From e82b0b98a51b5d29f5fdcce2d641e75282152a82 Mon Sep 17 00:00:00 2001 From: Logic769 Date: Fri, 30 Oct 2026 04:53:45 +0800 Subject: [PATCH 1/2] env: require a command when --argv0 is given --- src/uu/env/locales/en-US.ftl | 1 + src/uu/env/src/env.rs | 8 ++++++++ tests/by-util/test_env.rs | 15 +++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/uu/env/locales/en-US.ftl b/src/uu/env/locales/en-US.ftl index 60f9261816e..c6675cae817 100644 --- a/src/uu/env/locales/en-US.ftl +++ b/src/uu/env/locales/en-US.ftl @@ -33,6 +33,7 @@ env-error-use-s-shebang = use -[v]S to pass options in shebang lines env-error-cannot-unset = cannot unset '{ $name }': Invalid argument env-error-cannot-unset-invalid = cannot unset { $name }: Invalid argument env-error-must-specify-command-with-chdir = must specify command with --chdir (-C) +env-error-must-specify-command-with-argv0 = must specify command with --argv0 (-a) env-error-cannot-change-directory = cannot change directory to { $directory }: { $error } env-error-argv0-not-supported = --argv0 is currently not supported on this platform env-error-failed-set-signal-action = failed to set signal action for signal { $signal }: { $error } diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 61efcbcaf99..54d2b0533e0 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -1039,6 +1039,14 @@ fn apply_change_directory(opts: &Options<'_>) -> Result<(), Box> { )); } + // GNU env requires a command when --argv0 is given. + if opts.program.is_empty() && opts.argv0.is_some() { + return Err(UUsageError::new( + 125, + translate!("env-error-must-specify-command-with-argv0"), + )); + } + if let Some(d) = opts.running_directory { match env::set_current_dir(d) { Ok(()) => d, diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index 3599e9a6012..975b428c6b6 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -477,6 +477,21 @@ fn test_fail_change_directory() { assert!(out.contains("env: cannot change directory to ")); } +#[test] +fn test_argv0_without_command_fails() { + let out = new_ucmd!() + .args(&["-a", "foo"]) + .fails_with_code(125) + .stderr_move_str(); + assert!(out.contains("must specify command with --argv0 (-a)")); + + let out = new_ucmd!() + .args(&["--argv0", "foo"]) + .fails_with_code(125) + .stderr_move_str(); + assert!(out.contains("must specify command with --argv0 (-a)")); +} + #[cfg(not(windows))] // windows has no executable "echo", its only supported as part of a batch-file #[test] fn test_split_string_into_args_one_argument_no_quotes() { From b2e56101728174b4131bdd883efbcf4c77f3a9ea Mon Sep 17 00:00:00 2001 From: Logic769 Date: Mon, 2 Nov 2026 10:15:49 +0800 Subject: [PATCH 2/2] fix(env): validate --argv0 before changing directory --- src/uu/env/src/env.rs | 17 +++++++++-------- tests/by-util/test_env.rs | 18 +++++++----------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 54d2b0533e0..1ba7aa9b71f 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -814,6 +814,15 @@ impl EnvAppData { } } + // GNU env requires a command when --argv0 is given. Validate before + // apply_change_directory performs its side effect. + if opts.program.is_empty() && opts.argv0.is_some() { + return Err(UUsageError::new( + 125, + translate!("env-error-must-specify-command-with-argv0"), + )); + } + apply_change_directory(&opts)?; if opts.program.is_empty() { // no program provided, so just dump all env vars to stdout @@ -1039,14 +1048,6 @@ fn apply_change_directory(opts: &Options<'_>) -> Result<(), Box> { )); } - // GNU env requires a command when --argv0 is given. - if opts.program.is_empty() && opts.argv0.is_some() { - return Err(UUsageError::new( - 125, - translate!("env-error-must-specify-command-with-argv0"), - )); - } - if let Some(d) = opts.running_directory { match env::set_current_dir(d) { Ok(()) => d, diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index 975b428c6b6..cbc4f387cc0 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -479,17 +479,13 @@ fn test_fail_change_directory() { #[test] fn test_argv0_without_command_fails() { - let out = new_ucmd!() - .args(&["-a", "foo"]) - .fails_with_code(125) - .stderr_move_str(); - assert!(out.contains("must specify command with --argv0 (-a)")); - - let out = new_ucmd!() - .args(&["--argv0", "foo"]) - .fails_with_code(125) - .stderr_move_str(); - assert!(out.contains("must specify command with --argv0 (-a)")); + for arg in ["-a", "--argv0"] { + let out = new_ucmd!() + .args(&[arg, "foo"]) + .fails_with_code(125) + .stderr_move_str(); + assert!(out.contains("must specify command with --argv0 (-a)")); + } } #[cfg(not(windows))] // windows has no executable "echo", its only supported as part of a batch-file