Skip to content

Commit a2d29c0

Browse files
oech3sylvestre
authored andcommitted
dd: fix OOM panic with skip=1
1 parent aa9332b commit a2d29c0

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/uu/dd/src/dd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ impl Num {
185185
/// Returns the total number of bytes actually read.
186186
fn read_and_discard<R: Read>(reader: &mut R, n: u64, buf_size: usize) -> io::Result<u64> {
187187
// todo: consider splice()ing to /dev/null on Linux
188-
let mut buf = Vec::with_capacity(buf_size);
188+
let mut buf = Vec::new();
189+
buf.try_reserve(buf_size.min(n as usize))?; // try_with_capacity is unstable <https://github.com/rust-lang/rust/issues/91913>
189190
let mut total = 0u64;
190191
let mut remaining = n;
191192
while remaining > 0 {

tests/by-util/test_dd.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ fn test_out_of_memory() {
123123
.stderr_contains("memory"); //todo: improve error message at all platforms
124124
}
125125

126+
#[test]
127+
fn test_out_of_memory_skip() {
128+
new_ucmd!()
129+
.arg("bs=1PB")
130+
.arg("skip=1")
131+
.fails_with_code(1)
132+
.stderr_contains("memory");
133+
}
134+
126135
#[test]
127136
fn test_stdin_stdout() {
128137
let input = build_ascii_block(521);

0 commit comments

Comments
 (0)