Skip to content

undefined behaviour in std::ascii due to implementation defined struct layout #18314

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

Closed
thestinger opened this issue Oct 25, 2014 · 2 comments · Fixed by #18366
Closed

undefined behaviour in std::ascii due to implementation defined struct layout #18314

thestinger opened this issue Oct 25, 2014 · 2 comments · Fixed by #18366

Comments

@thestinger
Copy link
Contributor

There are 3 cases of this undefined behaviour in std::ascii:

    unsafe fn into_ascii_nocheck(self) -> Vec<Ascii> {
        let v: Vec<u8> = mem::transmute(self);
        v.into_ascii_nocheck()
    }
    #[inline]
    unsafe fn into_ascii_nocheck(self) -> Vec<Ascii> {
        mem::transmute(self)
    }
    fn into_bytes(self) -> Vec<u8> {
        unsafe { mem::transmute(self) }
    }

There is no guarantee that Vec<T> and Vec<U> have the same layout under the new rules. This needs to manually copy over the fields one by one which would compile to the same code as long as a feature like struct randomization wasn't enabled. Since vectors expose from_raw_parts, it could be switched to make use of that. However, this is a pervasive issue with transmute in the standard libraries.

@aochagavia
Copy link
Contributor

@thestinger I would like to fix this one. However, I don't understand why you would need to use from_raw_parts. If we need to copy everything, why don't we do it directly to a Vec?

@reem
Copy link
Contributor

reem commented Oct 26, 2014

@aochagavia the fields of Vec are private, so you can't just use Vec { ... }

SimonSapin pushed a commit to tomprogrammer/rust-ascii that referenced this issue Nov 27, 2014
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

Successfully merging a pull request may close this issue.

3 participants