From ac545e040beb4aa8c44fc3897b88bc03dab6cc7d Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Sun, 29 Nov 2026 20:16:44 +0300 Subject: [PATCH 1/2] sed: reject repeated p option to substitute command GNU sed rejects an s command that repeats the p flag with "multiple `p' options to `s' command" and exit status 1. uutils sed previously accepted it and set print_flag again. Check if subst.print_flag is already set when encountering 'p' in compile_subst_flags and raise a compilation error, matching repeated 'g' and repeated numbers. Fixes #652 --- src/sed/compiler.rs | 5 +++++ tests/by-util/test_sed.rs | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index 342db035..bdcb428a 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -1248,6 +1248,9 @@ pub fn compile_subst_flags( } 'p' => { + if subst.print_flag { + return compilation_error(lines, line, "multiple `p' options to `s' command"); + } subst.print_flag = true; // 'p' is applied before 'e' iff 'e' has not been seen yet. subst.p_before_e = !subst.execute; @@ -2734,6 +2737,8 @@ mod tests { #[rstest] #[case::multiple_g("gpg", ":3: error: multiple `g' options to `s' command")] + #[case::multiple_p("pgp", ":3: error: multiple `p' options to `s' command")] + #[case::multiple_p_adjacent("pp", ":2: error: multiple `p' options to `s' command")] #[case::multiple_numbers("2g3", ":3: error: multiple number options to `s' command")] #[case::w_missing_filename("w ", "missing file path")] #[case::invalid_flag("z", "invalid substitute flag")] diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index 7c408df2..88c4fe3e 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -1135,6 +1135,18 @@ fn test_subst_flags_pe_print_then_execute() { .stdout_is("echo hi\nhi\n"); } +#[rstest] +#[case::adjacent("s/a/b/pp")] +#[case::separated("s/a/b/pgp")] +fn test_subst_repeated_p_flag_rejected(#[case] script: &str) { + new_ucmd!() + .arg(script) + .pipe_in("a\n") + .fails() + .code_is(1) + .stderr_contains("multiple `p' options to `s' command"); +} + #[test] fn test_subst_e_flag_rejected_with_posix() { // e flag is rejected at compile time if --posix or --sandbox is provided. From e6e823d081e09441c5e97ce681dbb312d5adf764 Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Sun, 29 Nov 2026 20:55:01 +0300 Subject: [PATCH 2/2] tests: don't pipe stdin into sed for compilation error test --- tests/by-util/test_sed.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index 88c4fe3e..d5c49831 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -1141,7 +1141,6 @@ fn test_subst_flags_pe_print_then_execute() { fn test_subst_repeated_p_flag_rejected(#[case] script: &str) { new_ucmd!() .arg(script) - .pipe_in("a\n") .fails() .code_is(1) .stderr_contains("multiple `p' options to `s' command");