Skip to content

Commit b0d504c

Browse files
vitkyrkacakebaker
authored andcommitted
sort: fix infinite loop on --files0-from read error
If the read from the `--files0-from` file results in a permanent error, we end up in an infinite loop.
1 parent 358063f commit b0d504c

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/uu/sort/src/sort.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
20422042
// sort errors with "cannot open: [...]" instead of "cannot read: [...]" here
20432043
let reader = open_with_open_failed_error(&files0_from)?;
20442044
let buf_reader = BufReader::new(reader);
2045-
for (line_num, line) in buf_reader.split(b'\0').flatten().enumerate() {
2045+
for (line_num, line_res) in buf_reader.split(b'\0').enumerate() {
2046+
let line = line_res.map_err(|error| SortError::ReadFailed {
2047+
path: files0_from.clone(),
2048+
error,
2049+
})?;
20462050
let f = std::str::from_utf8(&line)
20472051
.expect("Could not parse string from zero terminated input.");
20482052
match f {

tests/by-util/test_sort.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,15 @@ fn test_files0_from_empty() {
15691569
.stderr_only("sort: no input from 'file'\n");
15701570
}
15711571

1572+
#[test]
1573+
#[cfg(unix)]
1574+
fn test_files0_read_error() {
1575+
new_ucmd!()
1576+
.args(&["--files0-from", "."])
1577+
.fails_with_code(2)
1578+
.stderr_only("sort: cannot read: .: Is a directory\n");
1579+
}
1580+
15721581
#[cfg(target_os = "linux")]
15731582
#[test]
15741583
// Test for GNU tests/sort/sort-files0-from.pl "empty-non-regular"

0 commit comments

Comments
 (0)