Skip to content

Commit 39962d6

Browse files
rcseacordmanhatsu
authored andcommitted
Update types-and-traits.rst
interim save
1 parent 2b9d1f7 commit 39962d6

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/coding-guidelines/types-and-traits/gui_UnionFieldValidity.rst.inc

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
that type must satisfy [VALID]_.
3636
Reading a union field performs a *typed read*,
3737
which asserts that the bytes are valid for the target type.
38-
Violating this invariant is undefined behavior.
3938
4039
Examples of validity requirements for common types:
4140
@@ -46,19 +45,14 @@
4645
* **Floating point**: All bit patterns are valid for the ``f32`` or ``f64``.type
4746
* **Integers**: All bit patterns are valid for integer types.
4847
49-
Consequences of reading invalid values include:
50-
51-
* Immediate undefined behavior, even if the value is not used
52-
* Miscompilation due to compiler assumptions about valid values
53-
* Security vulnerabilities from unexpected program behavior
54-
* Non-deterministic behavior that varies across optimization levels or platforms
48+
Reading an invalid value is undefined behavior.
5549
5650
.. non_compliant_example::
5751
:id: non_compl_ex_UnionBool
5852
:status: draft
5953
60-
This example reads a boolean from a union field containing an invalid bit pattern.
61-
The value ``3`` is not a valid boolean (only ``0`` and ``1`` are valid).
54+
This noncompliant example reads a Boolean from a union field containing an invalid bit pattern.
55+
The value ``3`` is not a valid Boolean (only ``0`` and ``1`` are valid).
6256
6357
.. code-block:: rust
6458
@@ -70,15 +64,15 @@
7064
fn main() {
7165
let u = IntOrBool { i: 3 };
7266
73-
// Noncompliant: reading bool field with invalid value (3)
74-
let invalid_bool = unsafe { u.b }; // UB: 3 is not a valid bool
67+
// Undefined behavior reading an invalid value from a union field of type 'bool'
68+
unsafe { u.b }; // Noncompliant
7569
}
7670
7771
.. non_compliant_example::
7872
:id: non_compl_ex_UnionChar
7973
:status: draft
8074
81-
This example reads a ``char`` from a union containing an invalid Unicode value.
75+
This noncompliant example reads a ``char`` from a ``union`` containing an invalid Unicode value.
8276
8377
.. code-block:: rust
8478
@@ -88,18 +82,18 @@
8882
}
8983
9084
fn main() {
91-
// 0xD800 is a surrogate, not a valid Unicode scalar value
85+
// 0xD800 is a surrogate and not a valid Unicode scalar value
9286
let u = IntOrChar { i: 0xD800 };
9387
94-
// Non-compliant: reading char field with invalid Unicode value
95-
let invalid_char = unsafe { u.c }; // UB: surrogates are not valid chars
88+
// Noncompliant: reading an invalid Unicode value from a union field of type 'char'
89+
unsafe { u.c }; // Noncompliant
9690
}
9791
9892
.. non_compliant_example::
9993
:id: non_compl_ex_UnionEnum
10094
:status: draft
10195
102-
This example reads an enum from a ``union`` containing an invalid discriminant.
96+
This noncompliant example reads an invalid discriminant from a ``union`` field of 'Color' enumeration type.
10397
10498
.. code-block:: rust
10599
@@ -119,15 +113,15 @@
119113
fn main() {
120114
let u = IntOrColor { i: 42 };
121115
122-
// Noncompliant: 42 is not a valid Color discriminant
123-
let invalid_color = unsafe { u.c }; // UB: no Color variant for 42
116+
// Undefined behavior reading an invalid discriminant from the 'Color' enumeration type
117+
unsafe { u.c }; // Noncompliant
124118
}
125119
126120
.. non_compliant_example::
127121
:id: non_compl_ex_UnionRef
128122
:status: draft
129123
130-
This example reads a reference from a ``union`` containing a null or misaligned pointer.
124+
This noncompliant example reads a reference from a ``union`` containing a null or misaligned pointer.
131125
132126
.. code-block:: rust
133127
@@ -147,7 +141,7 @@
147141
:id: compl_ex_UnionTrackField
148142
:status: draft
149143

150-
Track the active field explicitly to ensure valid reads.
144+
This compliant example tracks the active field explicitly to ensure valid reads.
151145

152146
.. code-block:: rust
153147

@@ -202,7 +196,7 @@
202196
:id: compl_ex_UnionSameField
203197
:status: draft
204198

205-
Read from the same field that was written.
199+
This compliant solution read from the same field that was written.
206200

207201
.. code-block:: rust
208202

@@ -223,7 +217,7 @@
223217
:id: compl_ex_UnionValidReinterpret
224218
:status: draft
225219

226-
Reinterpret between types where all bit patterns are valid.
220+
This compliant example reinterprets the value as a different types where all bit patterns are valid.
227221

228222
.. code-block:: rust
229223

@@ -250,7 +244,7 @@
250244
:id: compl_ex_UnionValidateBool
251245
:status: draft
252246

253-
Validate bytes before reading as a constrained type.
247+
This compliant solution validates bytes before reading as a constrained type.
254248

255249
.. code-block:: rust
256250

@@ -286,7 +280,7 @@
286280

287281
.. list-table::
288282
:header-rows: 0
289-
:widths: 5 85
283+
:widths: 5 60
290284

291285
* - .. [UNION]
292286
- The Rust Project Developers. "Rust Reference: Unions." *The Rust Reference*, n.d. https://doc.rust-lang.org/reference/items/unions.html.

0 commit comments

Comments
 (0)