Skip to content
Snippets Groups Projects
Commit b4abd143 authored by Carl Lerche's avatar Carl Lerche
Browse files

Mark `MutBuf::mut_buf` as unsafe

`MutBuf::mut_buf` allows access to uninitialized memory.
parent 296cac26
No related branches found
No related tags found
No related merge requests found
...@@ -321,7 +321,7 @@ impl MutBuf for MutByteBuf { ...@@ -321,7 +321,7 @@ impl MutBuf for MutByteBuf {
self.buf.advance(cnt) self.buf.advance(cnt)
} }
fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8] { unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8] {
let pos = self.buf.pos(); let pos = self.buf.pos();
let lim = self.buf.lim(); let lim = self.buf.lim();
&mut self.buf.mem.bytes_mut()[pos..lim] &mut self.buf.mem.bytes_mut()[pos..lim]
......
...@@ -102,7 +102,9 @@ pub trait MutBuf : Sized { ...@@ -102,7 +102,9 @@ pub trait MutBuf : Sized {
/// Returns a mutable slice starting at the current MutBuf position and of /// Returns a mutable slice starting at the current MutBuf position and of
/// length between 0 and `MutBuf::remaining()`. /// length between 0 and `MutBuf::remaining()`.
fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8]; ///
/// The returned byte slice may represent uninitialized memory.
unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8];
/// Write bytes from the given slice into the `MutBuf` and advance the /// Write bytes from the given slice into the `MutBuf` and advance the
/// cursor by the number of bytes written. /// cursor by the number of bytes written.
...@@ -268,7 +270,7 @@ impl<'a, R: io::Read+'a> Source for &'a mut R { ...@@ -268,7 +270,7 @@ impl<'a, R: io::Read+'a> Source for &'a mut R {
let mut cnt = 0; let mut cnt = 0;
while buf.has_remaining() { while buf.has_remaining() {
let i = try!(self.read(buf.mut_bytes())); let i = try!(self.read(unsafe { buf.mut_bytes() }));
if i == 0 { if i == 0 {
break; break;
...@@ -349,7 +351,7 @@ impl MutBuf for Vec<u8> { ...@@ -349,7 +351,7 @@ impl MutBuf for Vec<u8> {
} }
} }
fn mut_bytes(&mut self) -> &mut [u8] { unsafe fn mut_bytes(&mut self) -> &mut [u8] {
use std::slice; use std::slice;
if self.capacity() == self.len() { if self.capacity() == self.len() {
...@@ -359,10 +361,8 @@ impl MutBuf for Vec<u8> { ...@@ -359,10 +361,8 @@ impl MutBuf for Vec<u8> {
let cap = self.capacity(); let cap = self.capacity();
let len = self.len(); let len = self.len();
unsafe { let ptr = self.as_mut_ptr();
let ptr = self.as_mut_ptr(); &mut slice::from_raw_parts_mut(ptr, cap)[len..]
&mut slice::from_raw_parts_mut(ptr, cap)[len..]
}
} }
} }
......
...@@ -196,7 +196,7 @@ impl MutBuf for RingBuf { ...@@ -196,7 +196,7 @@ impl MutBuf for RingBuf {
self.advance_writer(cnt) self.advance_writer(cnt)
} }
fn mut_bytes(&mut self) -> &mut [u8] { unsafe fn mut_bytes(&mut self) -> &mut [u8] {
if self.cap == 0 { if self.cap == 0 {
return self.ptr.bytes_mut(); return self.ptr.bytes_mut();
} }
......
...@@ -53,7 +53,7 @@ impl<'a> MutBuf for MutSliceBuf<'a> { ...@@ -53,7 +53,7 @@ impl<'a> MutBuf for MutSliceBuf<'a> {
self.pos += cnt; self.pos += cnt;
} }
fn mut_bytes<'b>(&'b mut self) -> &'b mut [u8] { unsafe fn mut_bytes<'b>(&'b mut self) -> &'b mut [u8] {
&mut self.bytes[self.pos..] &mut self.bytes[self.pos..]
} }
} }
...@@ -208,17 +208,15 @@ impl<'a> Source for &'a Bytes { ...@@ -208,17 +208,15 @@ impl<'a> Source for &'a Bytes {
while src.has_remaining() && dst.has_remaining() { while src.has_remaining() && dst.has_remaining() {
let l; let l;
{ unsafe {
let s = src.bytes(); let s = src.bytes();
let d = dst.mut_bytes(); let d = dst.mut_bytes();
l = cmp::min(s.len(), d.len()); l = cmp::min(s.len(), d.len());
unsafe { ptr::copy_nonoverlapping(
ptr::copy_nonoverlapping( s.as_ptr(),
s.as_ptr(), d.as_mut_ptr(),
d.as_mut_ptr(), l);
l);
}
} }
src.advance(l); src.advance(l);
......
...@@ -30,7 +30,10 @@ pub fn test_vec_as_mut_buf() { ...@@ -30,7 +30,10 @@ pub fn test_vec_as_mut_buf() {
let mut buf = vec![]; let mut buf = vec![];
assert_eq!(buf.remaining(), usize::MAX); assert_eq!(buf.remaining(), usize::MAX);
assert_eq!(buf.mut_bytes().len(), 64);
unsafe {
assert_eq!(buf.mut_bytes().len(), 64);
}
buf.write(&b"zomg"[..]).unwrap(); buf.write(&b"zomg"[..]).unwrap();
......
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