Skip to content

Commit dfc08b2

Browse files
authored
dd: avoid 0-filling buf at read_and_discard (#11583)
1 parent bd69fe9 commit dfc08b2

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/uu/dd/src/dd.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,14 @@ impl Num {
184184
/// directly in `buf_size`-sized chunks, matching GNU dd's behavior.
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> {
187-
let mut buf = vec![0u8; buf_size];
187+
// todo: consider splice()ing to /dev/null on Linux
188+
let mut buf = Vec::with_capacity(buf_size);
188189
let mut total = 0u64;
189190
let mut remaining = n;
190-
191191
while remaining > 0 {
192-
let to_read = cmp::min(remaining, buf_size as u64) as usize;
193-
match reader.read(&mut buf[..to_read]) {
192+
let to_read = cmp::min(remaining, buf_size as u64);
193+
buf.clear();
194+
match reader.by_ref().take(to_read).read_to_end(&mut buf) {
194195
Ok(0) => break, // EOF
195196
Ok(bytes_read) => {
196197
total += bytes_read as u64;

0 commit comments

Comments
 (0)