Skip to content

Bitfield in C struct creates zero length arrray _bitfield_align_1 #3170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hannesweisbach opened this issue Mar 8, 2025 · 0 comments
Open

Comments

@hannesweisbach
Copy link

Hi,

I'm working on upgrading rust-bindgen used in the crate nrf-softdevice from 0.55.1 but I've hit a bit of a snag and i'm not sure what to do. I've recreated my issue using the C example code:

#define ARR_LEN 16

struct test
{
    unsigned char id[ARR_LEN];
    unsigned char bit1 : 1;
    unsigned char bit2 : 1;
    unsigned char bits : 6;
};

rust-bindgen 0.55.1 generates this code from this snipped:

pub const ARR_LEN: u32 = 16;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct test {
    pub id: [::std::os::raw::c_uchar; 16usize],
    pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>,
}

whereas with bindgen 0.71.1 I get the following code:

pub const ARR_LEN: u32 = 16;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct test {
    pub id: [::std::os::raw::c_uchar; 16usize],
    pub _bitfield_align_1: [u8; 0],
    pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize]>,
}

with the new _bitfiield_align_1 member.

_bitfield_align_1 needs to be initialized and thus breaks the API between old and new generated bindings. If the zero-length array is actually correct and intended I guess it is what it is, but I figure I ask before breaking user code.

Is _bitfield_align_1 as zero-length array intended to be there?
And just out of curiosity: How is it enforcing alignment? I'd figure an array u8 has a 1-byte alignment requirement anyway which is trivially fulfilled. But that is just my guess/intuition.

PS: the reason I need to update at all is to get the fix for #2312

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant