Skip to content
Snippets Groups Projects
Commit f693e038 authored by Stefan Bühler's avatar Stefan Bühler Committed by Carl Lerche
Browse files

Fix buffer overflow in Sink for Vec<u8>

Fixes #46
parent d0d27bd5
No related branches found
No related tags found
No related merge requests found
......@@ -407,12 +407,13 @@ impl<'a> Sink for &'a mut Vec<u8> {
self.clear();
let rem = buf.remaining();
let cap = self.capacity();
// Ensure that the vec is big enough
if rem > self.capacity() {
self.reserve(rem - cap);
// current length is 0, so reserve completely
self.reserve(rem);
}
debug_assert!(rem <= self.capacity());
unsafe {
{
......
use bytes::{Buf};
use byteorder;
use std::io::{Cursor};
use std::vec::{Vec};
#[test]
pub fn test_fresh_cursor_vec() {
......@@ -44,3 +45,15 @@ fn test_read_u16_buffer_underflow() {
let mut buf = Cursor::new(b"\x21");
buf.read_u16::<byteorder::BigEndian>();
}
#[test]
fn test_vec_sink_capacity() {
use bytes::Sink;
let mut sink: Vec<u8> = Vec::new();
sink.reserve(16);
assert!(sink.capacity() >= 16, "Capacity {} must be at least 16", sink.capacity());
let mut source = Cursor::new(b"0123456789abcdef0123456789abcdef");
sink.copy_from(&mut source);
assert!(sink.len() <= sink.capacity(), "Length {} must be less than or equal to capacity {}", sink.len(), sink.capacity());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment