-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
MaybeUninit::zeroed
and mem::zeroed
are the recommended way to initialize many of the datatypes in libc
(see: #41 (comment), #55 (comment), #58 (comment), #475 (comment), #1135 (comment), #1395 (comment)).
Per rust-lang/unsafe-code-guidelines#174, initialization from zeroed bytes is potentially unsound for structures with padding. The value of padding bytes is expressly undefined. Rust is therefore free to assume that padding bytes have a particular value. If Rust assumes that the padding bytes of a type T
have, for instance, the particular value of 42
, mem::zeroed()
will not produce a valid instance of T
. This is instant UB.
Many (but not all) struct definitions in libc
encode padding bytes explicitly as private fields. For such types, initialization via mem::zeroed()
is not UB. For the types defined using #[repr(align(N))]
to introduce padding bytes, initialization via mem::zeroed()
flirts with UB. This issue is relevant to #1324, for expanding the use of align(N)
.