Skip to content

Commit

Permalink
Store the size so we don't spiral into a deref loop
Browse files Browse the repository at this point in the history
  • Loading branch information
bitemyapp committed Dec 10, 2024
1 parent d836967 commit 055e44c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions rust/sword/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub enum AllocType {

pub(crate) enum Memory {
Mmap(MmapMut),
Malloc(*mut u8),
Malloc(*mut u8, usize),
}

impl Deref for Memory {
Expand All @@ -170,7 +170,9 @@ impl Deref for Memory {
fn deref(&self) -> &[u8] {
match self {
Memory::Mmap(mmap) => mmap.deref(),
Memory::Malloc(ptr) => unsafe { core::slice::from_raw_parts(*ptr, self.len()) },
Memory::Malloc(ptr, size) => {
unsafe { core::slice::from_raw_parts(*ptr, *size) }
},
}
}
}
Expand All @@ -180,7 +182,9 @@ impl DerefMut for Memory {
fn deref_mut(&mut self) -> &mut [u8] {
match self {
Memory::Mmap(mmap) => mmap.deref_mut(),
Memory::Malloc(ptr) => unsafe { core::slice::from_raw_parts_mut(*ptr, self.len()) },
Memory::Malloc(ptr, size) => unsafe {
core::slice::from_raw_parts_mut(*ptr, *size)
},
}
}
}
Expand Down Expand Up @@ -216,7 +220,7 @@ impl Memory {
// std promises that std::alloc::handle_alloc_error will diverge
std::alloc::handle_alloc_error(layout);
}
Self::Malloc(alloc)
Self::Malloc(alloc, size)
},
};
Ok(memory)
Expand Down

0 comments on commit 055e44c

Please sign in to comment.