rust-lang / rust-lang/rust-bindgen

Wrong type size on packed enum

Open
#3,195 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

I am working on a rust wrapper for a c library and there is one place I have to go and manually edit the bindgen file after generation. The problem is a packed enum that is then used in a struct. Bindgen is tagging the enum with #[repr(i32)], but because it is a packed enum, it should be #[repr(u8)] or similar. This causes the struct which uses the enum to be too large on the rust side and cause a segfault when attempting to access the enum member of the struct.

compiling on windows

a simplified version of the c file:

typedef __pragma(pack(push, 1)) enum __pragma(pack(pop)) {
    a,
    b,
    c,
} aaa;

typedef struct {
    /* other fields */
    aaa field;
} bbb;

I'm using rustified enums which work really well for my use case. after generation, I have to manually change the bindgen output file enum tag from repr(i32) to repr(u8) and I have to change the size of the struct from 88usize to 80usize and change the offset of the field from 80usize to 78usize

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 by reproducing the simplified packed-enum C case on Windows with rustified enums and inspect the generated bindgen file. Compare the enum representation, struct size, and field offset with the C layout; done means the generated Rust layout no longer requires manual edits and avoids the reported mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.