@@ -43,7 +43,6 @@ Types and Traits
4343 that type must satisfy [VALID ]_.
4444 Reading a union field performs a *typed read *,
4545 which asserts that the bytes are valid for the target type.
46- Violating this invariant is undefined behavior.
4746
4847 Examples of validity requirements for common types:
4948
@@ -54,19 +53,14 @@ Types and Traits
5453 * **Floating point **: All bit patterns are valid for the ``f32 `` or ``f64 ``.type
5554 * **Integers **: All bit patterns are valid for integer types.
5655
57- Consequences of reading invalid values include:
58-
59- * Immediate undefined behavior, even if the value is not used
60- * Miscompilation due to compiler assumptions about valid values
61- * Security vulnerabilities from unexpected program behavior
62- * Non-deterministic behavior that varies across optimization levels or platforms
56+ Reading an invalid value is undefined behavior.
6357
6458 .. non_compliant_example ::
6559 :id: non_compl_ex_UnionBool
6660 :status: draft
6761
68- This example reads a boolean from a union field containing an invalid bit pattern.
69- The value ``3 `` is not a valid boolean (only ``0 `` and ``1 `` are valid).
62+ This noncompliant example reads a Boolean from a union field containing an invalid bit pattern.
63+ The value ``3 `` is not a valid Boolean (only ``0 `` and ``1 `` are valid).
7064
7165 .. code-block :: rust
7266
@@ -78,15 +72,15 @@ Types and Traits
7872 fn main() {
7973 let u = IntOrBool { i: 3 };
8074
81- // Noncompliant: reading bool field with invalid value (3)
82- let invalid_bool = unsafe { u.b }; // UB: 3 is not a valid bool
75+ // Undefined behavior reading an invalid value from a union field of type 'bool'
76+ unsafe { u.b }; // Noncompliant
8377 }
8478
8579 .. non_compliant_example ::
8680 :id: non_compl_ex_UnionChar
8781 :status: draft
8882
89- This example reads a ``char `` from a union containing an invalid Unicode value.
83+ This noncompliant example reads a ``char `` from a `` union `` containing an invalid Unicode value.
9084
9185 .. code-block :: rust
9286
@@ -96,18 +90,18 @@ Types and Traits
9690 }
9791
9892 fn main() {
99- // 0xD800 is a surrogate, not a valid Unicode scalar value
93+ // 0xD800 is a surrogate and not a valid Unicode scalar value
10094 let u = IntOrChar { i: 0xD800 };
10195
102- // Non-compliant : reading char field with invalid Unicode value
103- let invalid_char = unsafe { u.c }; // UB: surrogates are not valid chars
96+ // Noncompliant : reading an invalid Unicode value from a union field of type 'char'
97+ unsafe { u.c }; // Noncompliant
10498 }
10599
106100 .. non_compliant_example ::
107101 :id: non_compl_ex_UnionEnum
108102 :status: draft
109103
110- This example reads an enum from a ``union `` containing an invalid discriminant .
104+ This noncompliant example reads an invalid discriminant from a ``union `` field of 'Color' enumeration type .
111105
112106 .. code-block :: rust
113107
@@ -127,15 +121,15 @@ Types and Traits
127121 fn main() {
128122 let u = IntOrColor { i: 42 };
129123
130- // Noncompliant: 42 is not a valid Color discriminant
131- let invalid_color = unsafe { u.c }; // UB: no Color variant for 42
124+ // Undefined behavior reading an invalid discriminant from the ' Color' enumeration type
125+ unsafe { u.c }; // Noncompliant
132126 }
133127
134128 .. non_compliant_example ::
135129 :id: non_compl_ex_UnionRef
136130 :status: draft
137131
138- This example reads a reference from a ``union `` containing a null or misaligned pointer.
132+ This noncompliant example reads a reference from a ``union `` containing a null or misaligned pointer.
139133
140134 .. code-block :: rust
141135
@@ -155,7 +149,7 @@ Types and Traits
155149 :id: compl_ex_UnionTrackField
156150 :status: draft
157151
158- Track the active field explicitly to ensure valid reads.
152+ This compliant example tracks the active field explicitly to ensure valid reads.
159153
160154 .. code-block :: rust
161155
@@ -210,7 +204,7 @@ Types and Traits
210204 :id: compl_ex_UnionSameField
211205 :status: draft
212206
213- Read from the same field that was written.
207+ This compliant solution read from the same field that was written.
214208
215209 .. code-block :: rust
216210
@@ -231,7 +225,7 @@ Types and Traits
231225 :id: compl_ex_UnionValidReinterpret
232226 :status: draft
233227
234- Reinterpret between types where all bit patterns are valid.
228+ This compliant example reinterprets the value as a different types where all bit patterns are valid.
235229
236230 .. code-block :: rust
237231
@@ -258,7 +252,7 @@ Types and Traits
258252 :id: compl_ex_UnionValidateBool
259253 :status: draft
260254
261- Validate bytes before reading as a constrained type.
255+ This compliant solution validates bytes before reading as a constrained type.
262256
263257 .. code-block :: rust
264258
@@ -294,7 +288,7 @@ Types and Traits
294288
295289 .. list-table ::
296290 :header-rows: 0
297- :widths: 5 85
291+ :widths: 5 60
298292
299293 * - .. [UNION]
300294 - The Rust Project Developers. "Rust Reference: Unions." *The Rust Reference *, n.d. https://doc.rust-lang.org/reference/items/unions.html.
0 commit comments