[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHk-=wh+RRhqgmpNN=WMz-4kkkcyNF0-a6NpRvxH9DjSTy9Ccg@mail.gmail.com>
Date: Wed, 6 Sep 2023 16:34:32 -0700
From: Linus Torvalds <torvalds@...ux-foundation.org>
To: dsterba@...e.cz
Cc: Arnaldo Carvalho de Melo <acme@...nel.org>,
Kent Overstreet <kent.overstreet@...ux.dev>,
linux-kernel@...r.kernel.org, linux-fsdevel@...r.kernel.org,
linux-bcachefs@...r.kernel.org
Subject: Re: [GIT PULL] bcachefs
On Wed, 6 Sept 2023 at 16:20, David Sterba <dsterba@...e.cz> wrote:
>
> I think I've always seen an int for enums, unless it was
> explicitly narrowed in the structure (:8) or by __packed attribute in
> the enum definition.
'int' is definitely the default (and traditional) behavior.
But exactly because enums can act very differently depending on
compiler options (and some of those may have different defaults on
different architectures), we should never ever have a bare 'enum' as
part of a structure in any UAPI.
In fact, having an enum as a bitfield is much better for that case.
Doing a quick grep shows that sadly people haven't realized that.
Now: using -fshort-enum can break a _lot_ of libraries exactly for
this kind of reason, so the kernel isn't unusual, and I don't know of
anybody who actually uses -fshort-enum. I'm mentioning -fshort-enum
not because it's likely to be used, but mainly because it's an easy
way to show some issues.
You can get very similar issues by just having unusual enum values. Doing
enum mynum { val = 0x80000000 };
does something special too.
I leave it to the reader to figure out, but as a hint it's basically
exactly the same issue as I was trying to show with my crazy
-fshort-enum example.
Linus
Powered by blists - more mailing lists