Re: [PATCH 1/2] x86/bootflag: Change some static functions to bool

From: Ingo Molnar
Date: Mon Feb 24 2025 - 14:00:18 EST



* Jiri Slaby <jirislaby@xxxxxxxxxx> wrote:

> On 24. 02. 25, 8:39, Uros Bizjak wrote:
> > The internal compiler representation of the following testcase:
> >
> > _Bool foo (int x) { return x; }
> >
> > is:
> >
> > --cut here--
> > _Bool foo (int x)
> > {
> > _Bool _2;
> >
> > <bb 2> [local count: 1073741824]:
> > _2 = x_1(D) != 0;
> > return _2;
>
> Yes, exactly as dictated by the C99 standard.
>
> > }
> > --cut here--
> >
> > For me, !!x in the source means that the change of types was
> > intentional. Surely, the compiler can do it by itself, so at the end
> > of the day, it is just a matter of personal taste.
>
> I've just learnt, that we even have that in CodingStyle:
> ===
> > 17) Using bool
> > --------------
> >
> > The Linux kernel bool type is an alias for the C99 _Bool type. bool values can
> > only evaluate to 0 or 1, and implicit or explicit conversion to bool
> > automatically converts the value to true or false. When using bool types the
> > !! construction is not needed, which eliminates a class of bugs.
> ===

This rule doesn't apply here, because the !! operation isn't done on
bool types: 'x' in the parity() function is an 'int'...

So this CodingStyle entry is a red herring, and the !! is absolutely
used in the kernel as an explicit marker of intentional type conversion
to bool.

Thanks,

Ingo