Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slices are unsound #96

Open
js2xxx opened this issue Nov 18, 2024 · 0 comments
Open

Slices are unsound #96

js2xxx opened this issue Nov 18, 2024 · 0 comments

Comments

@js2xxx
Copy link

js2xxx commented Nov 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant