rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.79k stars 261 forks source link

Possibly a typo on the "What Unsafe Rust Can Do" page? #299

Closed mexus closed 2 years ago

mexus commented 3 years ago

https://github.com/rust-lang/nomicon/blob/0c7e5bd1428e7838252bb57b7f0fbfda4ec82f02/src/what-unsafe-does.md

Here's a paragraph that caused my confusion:

A reference/pointer is "dangling" if it is null or not all of the bytes it points to are part of the same allocation (so in particular they all have to be part of some allocation). The span of bytes it points to is determined by the pointer value and the size of the pointee type. As a consequence, if the span is empty, "dangling" is the same as "non-null". Note that slices and strings point to their entire range, so it's important that the length metadata is never too large (in particular, allocations and therefore slices and strings cannot be bigger than isize::MAX bytes). If for some reason this is too cumbersome, consider using raw pointers.

So, the first sentence states:

A reference/pointer is "dangling" if it is null or ...

And the sentence about an empty span states:

... if the span is empty, "dangling" is the same as "non-null"

Isn't there a contradiction? Shouldn't it be more like "is the same as null" or "non-danling" in the latter sentence?

Or am I missing something?

Thanks!

JohnTitor commented 3 years ago

So, if we consider "null pointers ⊂ dangling pointers", the latter sentence should be reworded as you suggested. Let's trace the history. The latter one was written in https://github.com/rust-lang/nomicon/commit/98a71fde9fdff9641b1bb6a23c90d49f7267c37f, the wording was already confusing as we considered "null pointers ⊂ dangling pointers" in other places (see also some other rewording commits in the related PR). But rewording the latter might be confusing as "dangling pointers" generally, AFAIK, don't contain "null pointers". cc @RalfJung you are the original author of it, any thoughts?

RalfJung commented 3 years ago

I think it would be reasonable to say that the null pointer is dangling... or are you saying that is not standard terminology? I never thought much about this, TBH.

JohnTitor commented 2 years ago

or are you saying that is not standard terminology?

No I don't think (TBH, I'm not sure) it's standard, but after some googling, I think some may want to distinguish "dangling pointers" from "null pointers" from the point of view of "whether it was valid, i.e. pointing to an actual place before". Anyway, if it's not confusing, I'm happy to reword the part as Denis suggested (dangling is the same as "non-null" "null").

RalfJung commented 2 years ago

I think some may want to distinguish "dangling pointers" from "null pointers" from the point of view of "whether it was valid, i.e. pointing to an actual place before".

But then what about pointers like 16 as *const i32, that are neither null nor have they pointed to an actual place before?

JohnTitor commented 2 years ago

Hmm, I'm not sure, TBH. Anyway, I'm going just to reword it as mentioned above, I think it's the best way here.