Skip to content

Commit

Permalink
fix(buffer): fix incorrect resizing of BufReader
Browse files Browse the repository at this point in the history
Closes #715
  • Loading branch information
seanmonstar committed Jan 4, 2016
1 parent a4230eb commit 3a18e72
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ unsafe fn grow_zerofill(buf: &mut Vec<u8>, additional: usize) {
use std::ptr;
let len = buf.len();
buf.set_len(len + additional);
ptr::write_bytes(buf.as_mut_ptr(), 0, buf.len());
ptr::write_bytes(buf.as_mut_ptr().offset(len as isize), 0, buf.len());
}

impl<R: Read> Read for BufReader<R> {
Expand Down Expand Up @@ -151,4 +151,14 @@ mod tests {
assert_eq!(rdr.pos, 0);
assert_eq!(rdr.cap, 0);
}

#[test]
fn test_resize() {
let raw = b"hello world";
let mut rdr = BufReader::with_capacity(&raw[..], 5);
rdr.read_into_buf().unwrap();
assert_eq!(rdr.get_buf(), b"hello");
rdr.read_into_buf().unwrap();
assert_eq!(rdr.get_buf(), b"hello world");
}
}

0 comments on commit 3a18e72

Please sign in to comment.