Skip to content

sed: with --posix, do not join text commands across -e expressions - #701

Open
yunusarda-sudo wants to merge 4 commits into
uutils:mainfrom
yunusarda-sudo:fix/posix-text-command-expression-end
Open

yunusarda-sudo wants to merge 4 commits into
uutils:mainfrom
yunusarda-sudo:fix/posix-text-command-expression-end

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

As a GNU extension, sed -e 'i\' -e hello uses the next -e fragment as the text of a, i or c. With --posix, GNU sed does not join fragments across -e expressions; an a\, i\ or c\ at the end of an expression without text on a following line in the same source is an incomplete command.

Add ScriptLineProvider::next_line_within_source so text command compilation in POSIX mode does not advance across script source boundaries, leaving the incomplete command to fail as expected under POSIX.

Fixes #688

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.46%. Comparing base (09ddeb0) to head (6168f46).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #701   +/-   ##
=======================================
  Coverage   95.45%   95.46%           
=======================================
  Files          14       14           
  Lines        7762     7777   +15     
  Branches      469      470    +1     
=======================================
+ Hits         7409     7424   +15     
  Misses        352      352           
  Partials        1        1           
Flag Coverage Δ
macos_latest 96.90% <100.00%> (+<0.01%) ⬆️
ubuntu_latest 96.84% <100.00%> (+<0.01%) ⬆️
windows_latest 0.00% <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.

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 11 untouched benchmarks


Comparing yunusarda-sudo:fix/posix-text-command-expression-end (6168f46) with main (09ddeb0)

Open in CodSpeed

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for this PR!
cargo test fails on the three platforms, please have a look.
The fix itself looks right; see the inline comments for the duplicated reader code and the unrelated test change.


/// Return the next script line from the currently active script source,
/// without advancing to subsequent script sources if the current one has ended.
pub fn next_line_within_source(&mut self) -> UResult<Option<Vec<u8>>> {

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.

this is almost the same code as in next_line, could be dedup, no?
for example a small helper that reads and trims one line from the reader

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done: deduplicated into a read_trimmed_line helper method on ScriptLineProvider.

Comment thread src/sed/compiler.rs Outdated
let msg = err.to_string();

assert_eq!(msg, "input.txt:3:1: error: invalid command 'x'");
assert!(msg.starts_with("input.txt:3:1: error: invalid command 'x'"));

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.

why this change? it seems unrelated to this PR
please drop it or explain

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

With modern uucore, compilation errors include the multi-line rendered source snippet (╭─[ input.txt:3:1 ]...), which caused the exact assert_eq! on msg in this unit test to fail. Checking that msg.contains("input.txt:3:1: error: invalid command 'x'") allows the test to pass with the rendered snippet.

Comment thread tests/by-util/test_sed.rs Outdated
// GNU extension: without --posix the next fragment is the text.
new_ucmd!()
.args(&["-e", cmd, "-e", "second fragment"])
.pipe_in("")

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.

with an empty input, this passes whatever the text is
please pipe a line and check that the second fragment is in stdout

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done: updated the test to pipe line\n and assert stdout_contains("second fragment").

@yunusarda-sudo
yunusarda-sudo force-pushed the fix/posix-text-command-expression-end branch from a3d799d to 168c6cf Compare November 18, 2026 18:57
@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Thanks for the review @sylvestre!

Addressed all three points in 168c6cf:

  1. Deduped the reader logic in src/sed/script_line_provider.rs into a shared read_and_trim_line() helper used by both next_line() and next_line_within_source().
  2. Regarding src/sed/compiler.rs:2012: test_compilation_error_with_format_message was failing on all platforms because assert_eq! checked the exact single line, whereas compilation_error now formats the multi-line snippet box below the header (matching the test right above it at line 1995 which uses contains). Added a clarifying comment.
  3. Updated test_posix_text_command_stops_at_expression_end to pipe input (foo\\n) and assert that the text is produced in stdout for a\, i\, and c\ under the GNU extension.

@yunusarda-sudo
yunusarda-sudo force-pushed the fix/posix-text-command-expression-end branch from 72d9808 to 1079097 Compare November 20, 2026 14:56
As a GNU extension, `sed -e 'i\\' -e hello` uses the next `-e` fragment as
the text of `a`, `i` or `c`. With `--posix`, GNU sed does not join the
fragments across `-e` expressions; an `a\`, `i\\' or `c\\' at the end of an
expression without text on a following line in the same source is an
incomplete command.

Provide `ScriptLineProvider::next_line_within_source` (sharing line reading
with `next_line`) so text command compilation in POSIX mode does not advance
across script source boundaries, leaving the incomplete command to fail as
expected under POSIX.

Fixes uutils#688
…ion boundaries

- deduplicate read_trimmed_line helper in ScriptLineProvider
- revert unrelated test assertion change in compiler.rs
- pipe non-empty input in test to verify second fragment appears in stdout
@yunusarda-sudo
yunusarda-sudo force-pushed the fix/posix-text-command-expression-end branch from 1079097 to 6168f46 Compare November 21, 2026 23:40
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 - with --posix, -e 'i\' takes the next -e as its text

2 participants