Skip to content

sed: reject a repeated p flag in the substitute command - #659

Open
bfs2021 wants to merge 2 commits into
uutils:mainfrom
bfs2021:fix/subst-repeated-p-flag
Open

bfs2021 wants to merge 2 commits into
uutils:mainfrom
bfs2021:fix/subst-repeated-p-flag

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 9, 2026

Copy link
Copy Markdown

Problem

s/a/b/pp is accepted and prints the line, where GNU sed rejects a repeated p flag:

$ printf 'a\n' | ./target/debug/sed 's/a/b/pp'
b
b
$ printf 'a\n' | sed 's/a/b/pp'
sed: -e expression #1, char 8: multiple `p' options to `s' command

Fixes #652.

Root cause

In compile_subst_flags (src/sed/compiler.rs), the 'p' arm assigns subst.print_flag = true without checking whether it was already set. The 'g' arm and the occurrence-number arm both return a compilation error on a repeat, so p was the only flag that could be given twice.

Fix

Return the same compilation error GNU uses, multiple p' options to s' command, when print_flag is already set. Non-repeated flags, p combined with other flags (gp, ep, pe, p1), and the p_before_e ordering semantics are unchanged.

Testing

  • New regression test test_subst_repeated_p_flag_is_rejected in tests/by-util/test_sed.rs: fails on the unpatched tree (test reports b\nb, exit 0) and passes after the fix (exit 1 plus the GNU message).
  • cargo test --test tests subst: 56 passed.
  • Full cargo test: 344 passed, 2 pre-existing failures on Windows (test_e_command_no_arg_command_failure, test_e_command_with_arg_command_failure; both also fail on a clean checkout here, an unrelated shell-encoding issue).
  • cargo fmt --check and cargo clippy are clean.

`compile_subst_flags` set `print_flag` unconditionally, so `s/a/b/pp` was
accepted and printed the line. GNU sed rejects it with "multiple \`p
@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.25%. Comparing base (5e2821e) to head (4731839).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #659   +/-   ##
=======================================
  Coverage   95.24%   95.25%           
=======================================
  Files          14       14           
  Lines        7490     7503   +13     
  Branches      442      443    +1     
=======================================
+ Hits         7134     7147   +13     
  Misses        355      355           
  Partials        1        1           
Flag Coverage Δ
macos_latest 96.75% <ø> (+<0.01%) ⬆️
ubuntu_latest 96.68% <ø> (+<0.01%) ⬆️
windows_latest 0.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread tests/by-util/test_sed.rs
}

#[test]
fn test_subst_repeated_p_flag_is_rejected() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please make it a rstest with the separated case from the issue too (s/a/b/pgp)?

Comment thread tests/by-util/test_sed.rs Outdated
.arg("s/a/b/pp")
.fails()
.code_is(1)
.stderr_contains("multiple `p' options to `s' command");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check the position too, char 8: multiple ...

Comment thread tests/by-util/test_sed.rs Outdated

#[test]
fn test_subst_repeated_p_flag_is_rejected() {
// GNU sed rejects a repeated `p` flag instead of silently accepting it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test name already says it, this comment can go

The message alone does not pin down where the compiler stopped, so check
the full line (`<script argument 1>:1:8`) as the sibling tests do.
@bfs2021

bfs2021 commented Nov 10, 2026

Copy link
Copy Markdown
Author

Both applied in 4731839: the test now asserts the full diagnostic (<script argument 1>:1:8: error: multiple p' options to s' command), and the redundant comment is gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sed - s///pp (repeated p flag) is accepted instead of rejected

2 participants