libc icon indicating copy to clipboard operation
libc copied to clipboard

Using implicit padding on C FFI prevents Rust from writing / copying those bits

Open gnzlbg opened this issue 6 years ago • 2 comments

@sunfishcode pointed out here (https://github.com/rust-lang/libc/pull/1321#discussion_r277067911) that when we use explicit padding on C FFI:

struct Foo {
   _pad: u32;
  pub field: u16
}

Rust will insert copies, etc. for the padding bits, and when users write code like:

let foo = MaybeUninit::<Foo>::zeroed().assume_init();

those fields will be zeroed.

However, if we were to use

#[repr(align(4))] 
struct Foo {
    pub field: u16,
}

instead, this would not be the case.

We don't have that many types using repr(align) and repr(packed) explicitly, so we should manually check those against the C headers, and verify that they only use these attributes when C does so as well.

gnzlbg avatar Apr 20 '19 07:04 gnzlbg