Skip to content

Commit 6f57cf4

Browse files
authored
Merge pull request #1000 from RalfJung/raw-deref
clarify UB for raw ptr deref
2 parents 5669a43 + 5524a17 commit 6f57cf4

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/behavior-considered-undefined.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ code.
2323
</div>
2424

2525
* Data races.
26-
* Dereferencing (using the `*` operator on) a dangling or unaligned raw pointer.
26+
* Evaluating a [dereference expression] (`*expr`) on a raw pointer that is
27+
[dangling] or unaligned, even in [place expression context]
28+
(e.g. `addr_of!(&*expr)`).
2729
* Breaking the [pointer aliasing rules]. `&mut T` and `&T` follow LLVM’s scoped
2830
[noalias] model, except if the `&T` contains an [`UnsafeCell<U>`].
2931
* Mutating immutable data. All data inside a [`const`] item is immutable. Moreover, all
@@ -45,7 +47,7 @@ code.
4547
* A `!` (all values are invalid for this type).
4648
* An integer (`i*`/`u*`), floating point value (`f*`), or raw pointer obtained
4749
from [uninitialized memory][undef], or uninitialized memory in a `str`.
48-
* A reference or `Box<T>` that is dangling, unaligned, or points to an invalid value.
50+
* A reference or `Box<T>` that is [dangling], unaligned, or points to an invalid value.
4951
* Invalid metadata in a wide reference, `Box<T>`, or raw pointer:
5052
* `dyn Trait` metadata is invalid if it is not a pointer to a vtable for
5153
`Trait` that matches the actual dynamic trait the pointer or reference points to.
@@ -62,6 +64,15 @@ a restricted set of valid values. In other words, the only cases in which
6264
reading uninitialized memory is permitted are inside `union`s and in "padding"
6365
(the gaps between the fields/elements of a type).
6466

67+
> **Note**: Undefined behavior affects the entire program. For example, calling
68+
> a function in C that exhibits undefined behavior of C means your entire
69+
> program contains undefined behaviour that can also affect the Rust code. And
70+
> vice versa, undefined behavior in Rust can cause adverse affects on code
71+
> executed by any FFI calls to other languages.
72+
73+
### Dangling pointers
74+
[dangling]: #dangling-pointers
75+
6576
A reference/pointer is "dangling" if it is null or not all of the bytes it
6677
points to are part of the same allocation (so in particular they all have to be
6778
part of *some* allocation). The span of bytes it points to is determined by the
@@ -71,12 +82,6 @@ that slices and strings point to their entire range, so it is important that the
7182
metadata is never too large. In particular, allocations and therefore slices and strings
7283
cannot be bigger than `isize::MAX` bytes.
7384

74-
> **Note**: Undefined behavior affects the entire program. For example, calling
75-
> a function in C that exhibits undefined behavior of C means your entire
76-
> program contains undefined behaviour that can also affect the Rust code. And
77-
> vice versa, undefined behavior in Rust can cause adverse affects on code
78-
> executed by any FFI calls to other languages.
79-
8085
[`bool`]: types/boolean.md
8186
[`const`]: items/constant-items.md
8287
[noalias]: http://llvm.org/docs/LangRef.html#noalias
@@ -87,3 +92,5 @@ cannot be bigger than `isize::MAX` bytes.
8792
[Rustonomicon]: ../nomicon/index.html
8893
[`NonNull<T>`]: ../core/ptr/struct.NonNull.html
8994
[`NonZero*`]: ../core/num/index.html
95+
[dereference expression]: expressions/operator-expr.md#the-dereference-operator
96+
[place expression context]: expressions.md#place-expressions-and-value-expressions

0 commit comments

Comments
 (0)