UB in Rust vs C
UB in Rust vs C
Posted Aug 16, 2024 11:38 UTC (Fri) by mb (subscriber, #50428)In reply to: UB in Rust vs C by intelfx
Parent article: Standards for use of unsafe Rust in the kernel
There's no way to encode Null in a &T reference. It would effectively become Option<&T>, which is a different type.
So you can't construct &T from Null, because it can't hold the value.
The thing is impossible at construction time already.
So you can't construct &T from Null, because it can't hold the value.
The thing is impossible at construction time already.
In contrast to that, creating a raw pointer from anything can always hold the value. But you might not be allowed to deref it, because it's not pointing to valid memory, is unaligned or whatever. But the pointer *itself* would hold the intended bit pattern.
(This property is even exploited in some areas https://doc.rust-lang.org/std/ptr/fn.dangling.html)
