Closed
Description
As #134424 recently landed, for the following code, I'd expect the check will catch null reference as Miri does, but actually not:
// Based on the description of PR#134424, but using a ptr to ZST
let ptr: *const () = std::ptr::null();
let _val: &() = unsafe { &*ptr };
As per rust-lang/unsafe-code-guidelines#472 , access to a ptr to ZST is not UB, so *ptr
is ok.
$ cargo run
# **No panic** like "null pointer dereference occured"
But it could be better to catch null reference &*ptr
:
$ cargo miri run
error: Undefined Behavior: constructing invalid value: encountered a null reference
--> src/main.rs:3:30
|
3 | let _val: &() = unsafe { &*ptr };
| ^^^^^ constructing invalid value: encountered a null reference
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Not sure if I put up a real issue. I just thought the check would act on null reference, since in some cases the term pointer may refer to a reference as well.
I guess we need another check for null reference, because null ptr is not harmful, dereferencing to it is harmful, while it doesn't need dereferencing for null reference to be UB.