Skip to content

sort: with -m, copy standard input first when it reads the output file - #16686

Open
Xnadir wants to merge 1 commit into
uutils:mainfrom
Xnadir:sort-merge-stdin-is-output
Open

Xnadir wants to merge 1 commit into
uutils:mainfrom
Xnadir:sort-merge-stdin-is-output

Conversation

@Xnadir

@Xnadir Xnadir commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

sort -m -o ledger - updates < ledger truncated ledger while the merge was still reading it through standard input, so most of the records were lost. The check for an input that is the output file only compared path operands, and - was looked up as a file named -.

replace_output_file_in_input_files now also compares the file standard input is open on with the output file. If they are the same, it copies standard input from its current position into a temporary file of its own and merges that copy. It returns a list of copies, because a path operand and - may now both need one.

The new test is the issue's case with 10,000 records and fails on main. I also ran the issue's script by hand: main kept 4,097 of 10,001 lines here, and the fix keeps all of them. Standard input that was already partly read is copied from where it stands, and - ledger updates keeps both copies.

Closes #16679

AI disclosure: written with help from Claude Code.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 15:07

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/uu/sort/src/merge.rs
// than `fs::copy`, which would put the source's permission bits
// on the temporary file and undo the 0600 it was created with.
let (mut copy_file, copy_path) = tmp_dir.next_file()?;
if path_copy.is_none() {

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 stdin and path branches are almost the same code, could be dedup, no?

}

#[test]
fn test_merge_output_is_stdin() {

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 also cover - ledger updates (both copies) as a case? thanks

@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.49 MB (+104 KB, +0.07%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

This branch has not been deployed

No deployments
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.

sort -m silently truncates its input when stdin is redirected from the output file

3 participants