rust-lang / rust-lang/rust-bindgen

Bitfield in C struct creates zero length arrray _bitfield_align_1

Open
#3,170 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the C struct example in the issue and compare the generated bindings from bindgen 0.55.1 and 0.71.1, focusing on _bitfield_align_1. Trace how the zero-length array is produced and how it relates to bitfield alignment. Done means establishing whether the field is intentional and resolving or clearly documenting the generated API compatibility impact.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.