@@ -23,25 +23,25 @@ language cares about is preventing the following things:
23
23
* Causing a [ data race] [ race ]
24
24
* Executing code compiled with [ target features] [ ] that the current thread of execution does
25
25
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):
28
28
* 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
31
31
* a ` char ` outside the ranges [ 0x0, 0xD7FF] and [ 0xE000, 0x10FFFF]
32
32
* 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)
36
35
* slice metadata is invalid if the slice has a total size larger than
37
36
` isize::MAX ` bytes in memory
38
37
* ` dyn Trait ` metadata is invalid if it is not a pointer to a vtable for
39
38
` 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
41
40
* an integer (` i* ` /` u* ` ), floating point value (` f* ` ), or raw pointer read from
42
41
[ 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.)
45
45
46
46
"Producing" a value happens any time a value is assigned, passed to a
47
47
function/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
50
50
points to are part of the same allocation (so in particular they all have to be
51
51
part of * some* allocation). The span of bytes it points to is determined by the
52
52
pointer 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.
54
56
55
57
That's it. That's all the causes of Undefined Behavior baked into Rust. Of
56
58
course, unsafe functions and traits are free to declare arbitrary other
0 commit comments