Skip to content

Potential UB returning mutable bytes as MemoryBlock #11

Closed
@197g

Description

@197g

fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
let new_ptr = self.ptr.checked_sub(layout.size()).ok_or(AllocErr)?;
let aligned_ptr = new_ptr & !(layout.align() - 1);
if unlikely(aligned_ptr < self.start()) {
return Err(AllocErr);
}
let memory = self.create_block(aligned_ptr)?;
unsafe { init.init(memory) };
Ok(memory)

These lines return part of the data as a MemoryBlock. This unsafe operation permits the caller to write uninitialized bytes to that region. It's not yet clear if it is UB to do that. In any case it is a safety invariant so you mustn't leak this uninitialized state after your borrow of data has ended but there is no Drop implementation that would take care of it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions