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..d5c49831 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -1135,6 +1135,17 @@ 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) + .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.