Closed
Description
Currently we have no way of detecting that
#![feature(const_raw_ptr_deref)]
const X: &i32 = unsafe { &*(8 as *const i32) };
fn main() {}
is UB (an integer is not a valid safe address at compile-time as you can safely dereference it in another constant and then you'd get an error and UB if done at runtime.
The basic idea is to take the function at
and split it into two partsvalidate_scalar_by_type
which first matches on the scalar's type and then decides how to operate on it- the existing match should basically be pulled out and the correctness checks happen after one knows which type one is operating on
- new arm:
ty::Ref
can just useto_ptr()
on the value and convert the error into avalidation_failure!
error (see how this is done elsewhere in the same file)- do the pointer recursion only here
validate_scalar_by_layout
which pretty much does everything else that the currentvalidate_scalar
does, minus the type checks and pointer recursion.- run
validate_scalar_by_layout
on every scalar (maybe here?), and not just on leaf fields. This is necessary to catchconst FOO: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(0) };
because right now we're just checking the field ofNonZeroU8
, which isu8
and thus fine to be0
.
cc @RalfJung