runtime: fix a lifetime issue in lwip::Pbuf.

This would have allowed a use-after-move (or -free) to typecheck.
This commit is contained in:
whitequark 2016-12-05 05:23:37 +00:00
parent f68e4ae519
commit 668928a16c
2 changed files with 6 additions and 6 deletions

View File

@ -267,14 +267,14 @@ impl<'payload> Pbuf<'payload> {
unsafe { (*self.raw).len as usize }
}
pub fn as_slice(&self) -> &'payload [u8] {
pub fn as_slice(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts((*self.raw).payload as *const u8,
(*self.raw).len as usize)
}
}
pub fn as_mut_slice(&mut self) -> &'payload mut [u8] {
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe {
core::slice::from_raw_parts_mut((*self.raw).payload as *mut u8,
(*self.raw).len as usize)

View File

@ -447,10 +447,10 @@ impl<'a> Read for TcpStream<'a> {
}
let (pbuf, pos) = self.buffer.take().unwrap();
let slice = &pbuf.as_slice()[pos..];
let len = ::std::cmp::min(buf.len(), slice.len());
buf[..len].copy_from_slice(&slice[..len]);
if len < slice.len() {
let remaining = pbuf.len() - pos;
let len = ::std::cmp::min(buf.len(), remaining);
buf[..len].copy_from_slice(&pbuf.as_slice()[pos..pos + len]);
if len < remaining {
self.buffer = Some((pbuf, pos + len))
}
Ok(len)