Closed RalfJung closed 2 years ago
The offset_of macro at https://github.com/lumen/lumen/blob/521fda4d040a53ad53ed88174650c60959bdbcc8/liblumen_core/src/util.rs#L9 is not quite correct... it avoids creating dangling pointers, but it does create a reference to the uninitialized struct via &*u.as_ptr(). To avoid this, ptr::addr_of! should be used instead of &.
offset_of
&*u.as_ptr()
ptr::addr_of!
&
The memoffset crate provides a version of this macro that avoids UB (on older versions of rustc, this is not always possible, so it falls back to the 'least incorrect' version when needed).
@bitwalker is this being changed in your pending PR or should we track this?
Closing as completed, since that crate and macro have been removed in the rewrite
The
offset_of
macro at https://github.com/lumen/lumen/blob/521fda4d040a53ad53ed88174650c60959bdbcc8/liblumen_core/src/util.rs#L9 is not quite correct... it avoids creating dangling pointers, but it does create a reference to the uninitialized struct via&*u.as_ptr()
. To avoid this,ptr::addr_of!
should be used instead of&
.The memoffset crate provides a version of this macro that avoids UB (on older versions of rustc, this is not always possible, so it falls back to the 'least incorrect' version when needed).