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

This crate is unsoud #1

Closed
WaffleLapkin opened this issue Jan 18, 2021 · 3 comments
Closed

This crate is unsoud #1

WaffleLapkin opened this issue Jan 18, 2021 · 3 comments

Comments

@WaffleLapkin
Copy link

Despite the "Notes about safety" section in the readme, this crate is unsound because it allows creating a slice of different allocated objects. See this example of incorrect usage of core::slice::from_raw_parts in it's docs:

use std::slice;

fn join_slices<'a, T>(fst: &'a [T], snd: &'a [T]) -> &'a [T] {
    let fst_end = fst.as_ptr().wrapping_add(fst.len());
    let snd_start = snd.as_ptr();
    assert_eq!(fst_end, snd_start, "Slices must be contiguous!");
    unsafe {
        // The assertion above ensures `fst` and `snd` are contiguous, but they might
        // still be contained within _different allocated objects_, in which case
        // creating this slice is undefined behavior.
        slice::from_raw_parts(fst.as_ptr(), fst.len() + snd.len())
    }
}

fn main() {
    // `a` and `b` are different allocated objects...
    let a = 42;
    let b = 27;
    // ... which may nevertheless be laid out contiguously in memory: | a | b |
    let _ = join_slices(slice::from_ref(&a), slice::from_ref(&b)); // UB
}

Consider yanking all crate versions.

@golddranks
Copy link
Owner

I know; I wrote this as a sample/experiment for a RFC that was discussed at the time and found out to be unsound: rust-lang/rfcs#2806

I've been meaning to edit the README and yank this, but I forgot to. Thanks for reminding me. I'll do it this week.

@golddranks
Copy link
Owner

Done! Thanks for the reminder.

@WaffleLapkin
Copy link
Author

Thanks for the fast response!

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

2 participants