lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CANiq72ncROtm8Qkv_W95_Gigb1aVhq85zZ37itN-jHGDgTUJMA@mail.gmail.com>
Date: Mon, 18 Aug 2025 13:56:33 +0200
From: Miguel Ojeda <miguel.ojeda.sandonis@...il.com>
To: Danilo Krummrich <dakr@...nel.org>
Cc: Alice Ryhl <aliceryhl@...gle.com>, akpm@...ux-foundation.org, ojeda@...nel.org, 
	alex.gaynor@...il.com, boqun.feng@...il.com, gary@...yguo.net, 
	bjorn3_gh@...tonmail.com, lossin@...nel.org, a.hindborg@...nel.org, 
	tmgross@...ch.edu, abdiel.janulgue@...il.com, acourbot@...dia.com, 
	jgg@...pe.ca, lyude@...hat.com, robin.murphy@....com, 
	daniel.almeida@...labora.com, rust-for-linux@...r.kernel.org, 
	linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/4] rust: dma: implement DataDirection

On Mon, Aug 18, 2025 at 1:27 PM Danilo Krummrich <dakr@...nel.org> wrote:
>
> Does bindgen ever pick another type than i32 for C enums? If so, it'd be a
> downside that we'd have to mess with the type either in the `repr` or by casting
> the variants.

Yes, it can easily pick `u32` -- the variants are always `int` in C,
but the actual enum can be a type compatible with int, unsigned or
char; and `bindgen` generates each variant with a type alias to the
underlying type of the `enum`, not `int`.

So e.g. for

    enum uint_enum { uint_enum_a, uint_enum_b };
    enum int_enum { int_enum_a = -1, int_enum_b };

    _Static_assert(_Generic(uint_enum_a, int: 1, default: 0), "");
    _Static_assert(_Generic(uint_enum_b, int: 1, default: 0), "");
    _Static_assert(_Generic(int_enum_a, int: 1, default: 0), "");
    _Static_assert(_Generic(int_enum_b, int: 1, default: 0), "");

    _Static_assert(_Generic((enum uint_enum)0, unsigned: 1, default: 0), "");
    _Static_assert(_Generic((enum int_enum)0, int: 1, default: 0), "");

you get:

    pub const uint_enum_uint_enum_a: uint_enum = 0;
    pub const uint_enum_uint_enum_b: uint_enum = 1;
    pub type uint_enum = ffi::c_uint;

    pub const int_enum_int_enum_a: int_enum = -1;
    pub const int_enum_int_enum_b: int_enum = 0;
    pub type int_enum = ffi::c_int;

Then there is this issue I reported to be careful about, which can
change it back to `i32` if there is a forward reference:

    https://github.com/rust-lang/rust-bindgen/issues/3179

I hope that helps.

Cheers,
Miguel

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ