Skip to content

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

Open
@gnzlbg

Description

@gnzlbg

@sunfishcode pointed out here (#1321 (comment)) 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-mediumE-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions