dd: catch OOM

This commit is contained in:
oech3
2026-03-31 22:18:15 +02:00
committed by Sylvestre Ledru
parent 7ec8dc1f29
commit 0beca7cb84
2 changed files with 14 additions and 4 deletions
+6 -4
View File
@@ -1166,10 +1166,6 @@ fn dd_copy(mut i: Input, o: Output) -> io::Result<()> {
);
}
// Create a common buffer with a capacity of the block size.
// This is the max size needed.
let mut buf = vec![BUF_INIT_BYTE; bsize];
// Spawn a timer thread to provide a scheduled signal indicating when we
// should send an update of our progress to the reporting thread.
//
@@ -1201,6 +1197,11 @@ fn dd_copy(mut i: Input, o: Output) -> io::Result<()> {
BlockWriter::Unbuffered(o)
};
// Create a common empty buffer with a capacity of the block size.
// This is the max size needed.
let mut buf = Vec::new();
buf.try_reserve(bsize)?; // try_with_capacity is unstable https://github.com/rust-lang/rust/issues/91913
// The main read/write loop.
//
// Each iteration reads blocks from the input and writes
@@ -1366,6 +1367,7 @@ fn read_helper(i: &mut Input, buf: &mut Vec<u8>, bsize: usize) -> io::Result<Rea
// ------------------------------------------------------------------
// Read
// Resize the buffer to the bsize. Any garbage data in the buffer is overwritten or truncated, so there is no need to fill with BUF_INIT_BYTE first.
// resizing buf cause serious performance drop https://github.com/uutils/coreutils/issues/11544
buf.resize(bsize, BUF_INIT_BYTE);
let mut rstat = match i.settings.iconv.sync {
+8
View File
@@ -115,6 +115,14 @@ fn help() {
new_ucmd!().args(&["--help"]).succeeds();
}
#[test]
fn test_out_of_memory() {
new_ucmd!()
.arg("bs=1PB")
.fails_with_code(1)
.stderr_contains("memory"); //todo: improve error message at all platforms
}
#[test]
fn test_stdin_stdout() {
let input = build_ascii_block(521);