mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
dd: catch OOM
This commit is contained in:
+6
-4
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user