[PATCH] wireguard (gcc13): cast enum limits members to int in prints

David Laight David.Laight at ACULAB.COM
Tue Nov 1 09:39:22 UTC 2022


From: Jiri Slaby (SUSE)
> Sent: 31 October 2022 11:44
> 
> Since gcc13, each member of an enum has the same type as the enum [1]. And
> that is inherited from its members. Provided "REKEY_AFTER_MESSAGES = 1ULL
> << 60", the named type is unsigned long.
> 
> This generates warnings with gcc-13:
>   error: format '%d' expects argument of type 'int', but argument 6 has type 'long unsigned int'
> 
> Cast the enum members to int when printing them.
> 
> Alternatively, we can cast it to ulong (to silence gcc < 12) and use %lu.
> Alternatively, we can move REKEY_AFTER_MESSAGES away from the enum.

I'd suggest moving the 'out of range' value out of the enum.
Otherwise integer promotion to 'long' might happen elsewhere
and the effects might not be desirable.

It is a shame that gcc doesn't force you to add the type
to 'big enums' (or emit a warning) so that the behavioural
change is properly detected.

>From reading the gcc bug it seems that C++ has a syntax for that.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)



More information about the WireGuard mailing list