From fcf00a02a6df95221340dc1ea8b42f217d48af9f Mon Sep 17 00:00:00 2001 From: Expertcoderz Date: Mon, 24 Mar 2025 12:59:11 +0000 Subject: [PATCH 1/4] echo: fix duplication of `--` arguments Fixes #7558. Previously, a workaround for handling double hyphens had the erroneous side effect of echoing `--` twice in a row if the `--` is the first double hyphen to be passed on the argument list and it is also not the first argument to `echo`. This commit fixes the aforementioned issue. --- src/uu/echo/src/echo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 843b3040812..3028a4fa167 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -29,8 +29,8 @@ fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args { let mut result = Vec::new(); let mut is_first_double_hyphen = true; - for arg in args { - if arg == "--" && is_first_double_hyphen { + for (i, arg) in args.enumerate() { + if i < 2 && arg == "--" && is_first_double_hyphen { result.push(OsString::from("--")); is_first_double_hyphen = false; } From fb2a2764f6f1d8c2d0c1f94a6e1305476ccfcdbb Mon Sep 17 00:00:00 2001 From: Expertcoderz Date: Mon, 24 Mar 2025 13:38:07 +0000 Subject: [PATCH 2/4] test_echo: check for `--` arguments between This adds a test for passing `--` as a non-first argument to `echo`. Fixes #7558. --- tests/by-util/test_echo.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_echo.rs b/tests/by-util/test_echo.rs index d4430d05655..2af39bdc78d 100644 --- a/tests/by-util/test_echo.rs +++ b/tests/by-util/test_echo.rs @@ -243,7 +243,7 @@ fn test_hyphen_values_between() { } #[test] -fn test_double_hyphens() { +fn test_double_hyphens_at_start() { new_ucmd!().arg("--").succeeds().stdout_only("--\n"); new_ucmd!() .arg("--") @@ -252,6 +252,24 @@ fn test_double_hyphens() { .stdout_only("-- --\n"); } +#[test] +fn test_double_hyphens_between() { + new_ucmd!() + .arg("a") + .arg("--") + .arg("b") + .succeeds() + .stdout_only("a -- b\n"); + + new_ucmd!() + .arg("foo ") + .arg("-- --") + .arg("bar") + .arg("hehehe") + .succeeds() + .stdout_only("foo -- -- bar hehehe\n"); +} + #[test] fn wrapping_octal() { // Some odd behavior of GNU. Values of \0400 and greater do not fit in the From 34ea55c69e3682b3e2bd5ec487ec1fd8989f4323 Mon Sep 17 00:00:00 2001 From: Expertcoderz Date: Tue, 25 Mar 2025 07:15:24 +0000 Subject: [PATCH 3/4] test_echo: Fix spellcheck failure Adds a word to the `spell-checker:ignore` to prevent spellcheck from failing due to said word being introduced in fb2a2764f6f1d8c2d0c1f94a6e1305476ccfcdbb. --- tests/by-util/test_echo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/by-util/test_echo.rs b/tests/by-util/test_echo.rs index 2af39bdc78d..11676967493 100644 --- a/tests/by-util/test_echo.rs +++ b/tests/by-util/test_echo.rs @@ -2,7 +2,7 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (words) araba merci +// spell-checker:ignore (words) araba merci hehehe use crate::common::util::TestScenario; From 98252b47ef9bf077ea574b8d8f794401f5806b0e Mon Sep 17 00:00:00 2001 From: Expertcoderz Date: Wed, 26 Mar 2025 14:58:28 +0000 Subject: [PATCH 4/4] echo: fix omission of double-hyphen arguments Fixes a bug introduced by fcf00a02a6df95221340dc1ea8b42f217d48af9f as mentioned by @cakebaker. `echo -n -e --` no longer eats the double hyphen. However, this commit also reduces the double-hyphen duplication fix that fcf00a02a6df95221340dc1ea8b42f217d48af9f was supposed to introduce to a partial fix. The following example will cause double-hyphen duplication: ```sh $ cargo run -q echo -nonsense -- -nonsense -- -- ``` To fix that as well, a more involved workaround may be necessary. --- src/uu/echo/src/echo.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 3028a4fa167..7cab3f81afe 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -8,6 +8,7 @@ use clap::{Arg, ArgAction, ArgMatches, Command}; use std::env; use std::ffi::{OsStr, OsString}; use std::io::{self, StdoutLock, Write}; +use std::os::unix::ffi::OsStrExt; use uucore::error::{UResult, USimpleError}; use uucore::format::{parse_escape_only, EscapedChar, FormatChar, OctalParsing}; use uucore::{format_usage, help_about, help_section, help_usage}; @@ -28,11 +29,16 @@ mod options { fn handle_double_hyphens(args: impl uucore::Args) -> impl uucore::Args { let mut result = Vec::new(); let mut is_first_double_hyphen = true; + let mut option_index = 0; for (i, arg) in args.enumerate() { - if i < 2 && arg == "--" && is_first_double_hyphen { - result.push(OsString::from("--")); - is_first_double_hyphen = false; + if is_first_double_hyphen { + if i < option_index + 2 && arg == "--" { + result.push(OsString::from("--")); + is_first_double_hyphen = false; + } else if arg.as_bytes()[0] == b'-' { + option_index = i + } } result.push(arg); }