Stream: t-lang/wg-unsafe-code-guidelines

Topic: pointer comparisons


Wanja Hentze (May 07 2019 at 14:31, on Zulip):

So I do realize that, in C, comparing pointers that are not from the same allocation is undefined behavior.
In Rust, arbitrary pointers can be compared safely using either Eq or Ord.
But if I run that code in miri, I get an error.
Does that mean that comparisons of unrelated pointers are secretly UB? Or is this just something miri can not reason about?

Wanja Hentze (May 07 2019 at 14:35, on Zulip):

More concretely, should I be able to write the following function without UB?
fn is_in_bounds(ptr: *const T, slice: *const [T])

RalfJ (May 07 2019 at 20:31, on Zulip):

But if I run that code in miri, I get an error.

That is a limitation of Miri, not an indication of UB. The issues for that is https://github.com/rust-lang/miri/issues/224.

RalfJ (May 07 2019 at 20:32, on Zulip):

ptr comparison between any two pointers is safe in Rust.

RalfJ (May 07 2019 at 20:32, on Zulip):

there are some thorny questions around specifying that precisely though :/

Last update: Nov 20 2019 at 12:25UTC