File tree Expand file tree Collapse file tree 1 file changed +30
-3
lines changed Expand file tree Collapse file tree 1 file changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -37,14 +37,14 @@ Unsafety
37
37
38
38
.. code-block :: rust
39
39
40
- fn example_function() {
40
+ fn example_function() -> bool {
41
41
unsafe {
42
42
std::transmute<bool>(3_u8)
43
43
}
44
44
}
45
45
46
- A necessary condition to read the value behind a pointer is that it points to a live allocation.
47
- This is never the case for the live pointer, therefore reading a null pointer is undefined behavior.
46
+ A necessary condition to read the value behind a pointer is that it points to a valid allocation.
47
+ This is never the case for a null pointer, therefore reading it is undefined behavior.
48
48
See the safety precondition of :std: `std::ptr::read `.
49
49
50
50
.. code-block :: rust
@@ -55,4 +55,31 @@ Unsafety
55
55
}
56
56
}
57
57
58
+ .. compliant_example ::
59
+ :id: compl_ex_mt8h0T3BtONt
60
+ :status: draft
61
+
62
+ Since ``0_u8 `` is defined to represent the ``false `` value of bool, this example is free of
63
+ undefined behavior.
64
+
65
+ .. code-block :: rust
66
+
67
+ fn example_function() -> bool {
68
+ unsafe {
69
+ std::transmute<bool>(0_u8);
70
+ }
71
+ }
72
+
73
+ ``ptr `` points to a valid, aligned and properly initialized allocation.
74
+ Therefore, it satisfies all safety preconditions of :std: `std::ptr::read ` and can be read
75
+ without undefined behavior.
76
+
77
+ .. code-block :: rust
78
+
79
+ fn example_function() {
80
+ let ptr = Box::new(42).into_raw();
81
+ unsafe {
82
+ std::ptr::read(ptr);
83
+ }
84
+ }
58
85
You can’t perform that action at this time.
0 commit comments