@@ -23,25 +23,25 @@ language cares about is preventing the following things:
2323* Causing a [ data race] [ race ]
2424* Executing code compiled with [ target features] [ ] that the current thread of execution does
2525 not support
26- * Producing invalid primitive values (either alone or as a field of a compound
27- type such as ` enum ` /` struct ` /array/tuple):
26+ * Producing invalid values (either alone or as a field of a compound type such
27+ as ` enum ` /` struct ` /array/tuple):
2828 * a ` bool ` that isn't 0 or 1
29- * an undefined ` enum ` discriminant
30- * null ` fn ` pointers
29+ * an ` enum ` with an invalid discriminant
30+ * a null ` fn ` pointer
3131 * a ` char ` outside the ranges [ 0x0, 0xD7FF] and [ 0xE000, 0x10FFFF]
3232 * a ` ! ` (all values are invalid for this type)
33- * dangling/unaligned references, references that do themselves point to
34- invalid values, or wide references (to a dynamically sized type) with
35- invalid metadata
33+ * a reference that is dangling, unaligned, points to an invalid value, or
34+ that has invalid metadata (if wide)
3635 * slice metadata is invalid if the slice has a total size larger than
3736 ` isize::MAX ` bytes in memory
3837 * ` dyn Trait ` metadata is invalid if it is not a pointer to a vtable for
3938 ` Trait ` that matches the actual dynamic trait the reference points to
40- * a non-utf8 ` str `
39+ * a ` str ` that isn't valid UTF-8
4140 * an integer (` i* ` /` u* ` ), floating point value (` f* ` ), or raw pointer read from
4241 [ uninitialized memory] [ ]
43- * an invalid library type with custom invalid values, such as a ` NonNull ` or
44- the ` NonZero ` family of types, that is 0
42+ * a type with custom invalid values that is one of those values, such as a
43+ ` NonNull ` that is null. (Requesting custom invalid values is an unstable
44+ feature, but some stable libstd types, like ` NonNull ` , make use of it.)
4545
4646"Producing" a value happens any time a value is assigned, passed to a
4747function/primitive operation or returned from a function/primitive operation.
@@ -50,7 +50,9 @@ A reference/pointer is "dangling" if it is null or not all of the bytes it
5050points to are part of the same allocation (so in particular they all have to be
5151part of * some* allocation). The span of bytes it points to is determined by the
5252pointer value and the size of the pointee type. As a consequence, if the span is
53- empty, "dangling" is the same as "non-null".
53+ empty, "dangling" is the same as "non-null". Note that slices point to their
54+ entire range, so it's very important that the length metadata is never too
55+ large. If for some reason this is too cumbersome, consider using raw pointers.
5456
5557That's it. That's all the causes of Undefined Behavior baked into Rust. Of
5658course, unsafe functions and traits are free to declare arbitrary other
0 commit comments