Skip to content

Slices are unsound #96

Description

@js2xxx

The code below compiles without unsafe code but accesses memory out of the allocated range.

use stabby::slice::Slice;

fn main() {
    let v = vec![12, 34, 56];
    let mut s: Slice<'_, i32> = (&v[..]).into();
    println!("{:?}", s);
    println!("{:?} {:?}", s.start, s.len);
    s.start = s.start.map_addr(|a| a.checked_add(4).unwrap());
    println!("{:?}", s);
}

I think Slices and SliceMuts cannot expose their fields directly. Instead, equivalents to core::slice::from_raw_parts* and similar functions should be considered.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions