Skip to content

It would be nice to clarify "dangling" definition #521

Description

@Arthur-Milchior

The https://doc.rust-lang.org/nomicon/what-unsafe-does.html page define "dangling". Very quickly.
Searching online, I realize that many questions get asked about the exact definition of dangling in Rust, and as far as I can see, it's not totally clear that there is a single definition every rustaceans agree on.

Still,
it seems to clearly differs from the one on wikipedia, or from C++.
In particular, the Nomicon considers a null pointer to be dangling. If the definition in the nomicon is wrong, I guess it should be corrected. Otherwise, the difference with wikipedia and other language with pointer may need to be explicitly pointed out so that readers with such a background understand they should not defer to their previous knowledge.

My first problem is that you consider a null pointer to be dangling. The book seems to indicate that dangling means that the pointer used to be valid. Null pointers are never valid. In c++, a nullpointer is often used as a special valid value, that represents a pointer pointing to nothing. Kind of their way of representing a Option value. It would make no sense to call it dangling.

Access to a pointer, that may be null, is of course an issue. but issue does not seems to be danglyness.

The other issue is that even if all bytes the pointer points to belong to the same allocation, the pointer can still be dangling. After all, imagine that you have a pointer to a 1-byte int. But it points to something that currently contains the second byte of a 4-byte int. According to your definition, the pointer is not dangling. According what seems more usual in my experience, it's dangling. Since it refers to a value that is absolutely not what we expect to find there.

As a last, but least important, note. I guess I'd also make explicit what "bytes it points to" mean. My usual understanding of a pointer is that it's just some integer. More specifically, a pointer p points to a byte in memory, and p+1 points to the next byte, and so on. I guess it would need to be specified that a T* p points to all the bytes at address p (included) up to p+size(T) excluded.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions