Gilnaa / memoffset

offsetof for Rust
MIT License
224 stars 29 forks source link

Fix span_of #14

Closed RalfJung closed 5 years ago

RalfJung commented 5 years ago

Fix span_of in two ways: no more mem::uninitialized, and protect against deref coercions. This unfortunately has to drop support for nested fields and arrays, just like for offset_of.

Also share some code with offset_of, and update documentation.

Gilnaa commented 5 years ago

Thank you, this looks awesome.

Is there any reason why let_base_ptr declares a variable instead of returning it to the outer macro?

RalfJung commented 5 years ago

Yes: for the MaybeUninit case, it needs to point to another variable. We have to make sure that other variable lives long enough.

If we did

macro_rules! let_base_ptr {
    ($type:tt) => {{
        // No UB here, and the pointer does not dangle, either.
        // But we have to make sure that `uninit` lives long enough,
        // so it has to be in the same scope as `$name`.
        let uninit = $crate::mem::MaybeUninit::<$type>::uninit();
        uninit.as_ptr()
    }};
}

then the pointer returned by this macro would be dangling.

RalfJung commented 5 years ago

Thanks. :)

Now I think would be a good time for a new release, given all the soundness fixes for span_of and also for offset_of on old compilers.

Gilnaa commented 5 years ago

Sure, I'll try to do this today when I get home

On Jul 14, 2019, 11:05, at 11:05, Ralf Jung notifications@github.com wrote:

Thanks. :)

Now I think would be a good time for a new release, given all the soundness fixes for span_of and also for offset_of on old compilers.

-- You are receiving this because you modified the open/close state. Reply to this email directly or view it on GitHub: https://github.com/Gilnaa/memoffset/pull/14#issuecomment-511182339