xous: carton: leak memory when converting into_message()

When converting into a message, the associated memory should be leaked
and not freed when the Carton is dropped. This is because the Message
can outlive the MemoryMessage, and the Carton is then immediately
dropped.

Signed-off-by: Sean <xobs@ci.betrusted.io>
This commit is contained in:
Sean 2020-09-03 13:20:36 +08:00
parent 1b59719a74
commit eb725ec57f

View file

@ -8,6 +8,7 @@ pub struct Carton<'a> {
range: MemoryRange,
valid: MemoryRange,
slice: &'a [u8],
should_drop: bool,
}
impl<'a> Carton<'a> {
@ -36,10 +37,14 @@ impl<'a> Carton<'a> {
range: new_mem,
slice: unsafe { core::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), bytes.len()) },
valid,
should_drop: true,
}
}
pub fn into_message(self, id: usize) -> MemoryMessage {
pub fn into_message(mut self, id: usize) -> MemoryMessage {
// Leak the memory buffer, since it will be taken care of
// when the MemoryMessage is dropped.
self.should_drop = false;
MemoryMessage {
id,
buf: self.valid,
@ -86,6 +91,8 @@ impl<'a> AsRef<[u8]> for Carton<'a> {
impl<'a> Drop for Carton<'a> {
fn drop(&mut self) {
crate::unmap_memory(self.range).unwrap();
if self.should_drop {
crate::unmap_memory(self.range).unwrap();
}
}
}