Skip to content

Commit

Permalink
cat: fix #5186 by adding explicit flush. (#5256)
Browse files Browse the repository at this point in the history
* fix #5186 by adding explicit flush.
* make test machine-independent

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
  • Loading branch information
3 people authored Oct 6, 2024
1 parent bdaeac1 commit d8eb4e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,13 @@ fn write_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
}
stdout_lock.write_all(&buf[..n])?;
}

// If the splice() call failed and there has been some data written to
// stdout via while loop above AND there will be second splice() call
// that will succeed, data pushed through splice will be output before
// the data buffered in stdout.lock. Therefore additional explicit flush
// is required here.
stdout_lock.flush()?;
Ok(())
}

Expand Down
17 changes: 16 additions & 1 deletion tests/by-util/test_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore NOFILE nonewline
// spell-checker:ignore NOFILE nonewline cmdline

#[cfg(not(windows))]
use crate::common::util::vec_of_size;
Expand Down Expand Up @@ -529,6 +529,21 @@ fn test_dev_full_show_all() {
proc.kill();
}

// For some reason splice() on first of those files fails, resulting in
// fallback inside `write_fast`, the other splice succeeds, in effect
// without additional flush output gets reversed.
#[test]
#[cfg(target_os = "linux")]
fn test_write_fast_fallthrough_uses_flush() {
const PROC_INIT_CMDLINE: &str = "/proc/1/cmdline";
let cmdline = std::fs::read_to_string(PROC_INIT_CMDLINE).unwrap();

new_ucmd!()
.args(&[PROC_INIT_CMDLINE, "alpha.txt"])
.succeeds()
.stdout_only(format!("{cmdline}abcde\nfghij\nklmno\npqrst\nuvwxyz\n")); // spell-checker:disable-line
}

#[test]
#[cfg(unix)]
#[ignore]
Expand Down

0 comments on commit d8eb4e2

Please sign in to comment.