Skip to content

Possible unsound unsafe usages #1

Description

@charlesxsh

at version 0.2.1, src/lib.rs:195 and 203

#[inline]
    fn get(&self, offset: u32) -> &Trait {
        unsafe {
            // ! SAFETY: Trait object points to a valid byte representation of this type
            &*from_raw_parts(self.bytes.as_ptr().add(offset as usize).cast(), self.vtable)
        }
    }

    #[inline]
    fn get_mut(&mut self, offset: u32) -> &mut Trait {
        unsafe {
            // ! SAFETY: Trait object points to a valid byte representation of this type
            let ptr = self.bytes.as_mut_ptr().add(offset as usize).cast();
            &mut *from_raw_parts_mut(ptr, self.vtable)
        }
    }

Safety requirements in https://doc.rust-lang.org/std/primitive.pointer.html#method.add says that "If the computed offset is non-zero, then self must be derived from a pointer to some allocation, and the entire memory range between self and the result must be in bounds of that allocation.". This safety requirement does not hold since both functions accept parameter offset and used in add without checking.

Suggestions:

  1. Mark both functions as unsafe to notify developers/users the potential memory issues
  2. Add sufficient checks

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