Tim McNamara @timclicks.dev · Dec 2

Code review: do you know why this code triggers undefined behavior? play.rust-lang.org/?version=st...

10 likes 3 replies

?

Replies

AapoAlas · Dec 2

Bytes 1..=3 are uninitialised padding bytes, and cannot be read even through raw pointers.

Bjorn · Dec 2

I guess this code is about proving a point? As I don’t see the advantage the code in new() nor eq(). In the real world, would these actually be faster in any way?

Conrad Ludgate · Dec 2

Two things look sus to me. `(*ptr).cache_hit = false` is constructing a `&mut bool` to an uninit place. I usually use `(&raw mut (*ptr).cache_hit).write(false)` The second is that your equality check is reading uninit bytes, because padding bytes are always uninit