Skip to content

Commit 0beca7c

Browse files
oech3sylvestre
authored andcommitted
dd: catch OOM
1 parent 7ec8dc1 commit 0beca7c

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/uu/dd/src/dd.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,6 @@ fn dd_copy(mut i: Input, o: Output) -> io::Result<()> {
11661166
);
11671167
}
11681168

1169-
// Create a common buffer with a capacity of the block size.
1170-
// This is the max size needed.
1171-
let mut buf = vec![BUF_INIT_BYTE; bsize];
1172-
11731169
// Spawn a timer thread to provide a scheduled signal indicating when we
11741170
// should send an update of our progress to the reporting thread.
11751171
//
@@ -1201,6 +1197,11 @@ fn dd_copy(mut i: Input, o: Output) -> io::Result<()> {
12011197
BlockWriter::Unbuffered(o)
12021198
};
12031199

1200+
// Create a common empty buffer with a capacity of the block size.
1201+
// This is the max size needed.
1202+
let mut buf = Vec::new();
1203+
buf.try_reserve(bsize)?; // try_with_capacity is unstable https://github.com/rust-lang/rust/issues/91913
1204+
12041205
// The main read/write loop.
12051206
//
12061207
// 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
13661367
// ------------------------------------------------------------------
13671368
// Read
13681369
// 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.
1370+
// resizing buf cause serious performance drop https://github.com/uutils/coreutils/issues/11544
13691371
buf.resize(bsize, BUF_INIT_BYTE);
13701372

13711373
let mut rstat = match i.settings.iconv.sync {

tests/by-util/test_dd.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ fn help() {
115115
new_ucmd!().args(&["--help"]).succeeds();
116116
}
117117

118+
#[test]
119+
fn test_out_of_memory() {
120+
new_ucmd!()
121+
.arg("bs=1PB")
122+
.fails_with_code(1)
123+
.stderr_contains("memory"); //todo: improve error message at all platforms
124+
}
125+
118126
#[test]
119127
fn test_stdin_stdout() {
120128
let input = build_ascii_block(521);

0 commit comments

Comments
 (0)