Re: older gccs and case labels producing integer constants

From: Richard Biener
Date: Tue Apr 05 2022 - 18:55:10 EST


On Tue, 5 Apr 2022, Michael Matz wrote:

> Hey,
>
> On Tue, 5 Apr 2022, Peter Zijlstra wrote:
>
> > > sound/usb/midi.c: In function ‘snd_usbmidi_out_endpoint_create’:
> > > sound/usb/midi.c:1389:2: error: case label does not reduce to an integer constant
> > > case (((0xfc08) << 16) | (0x0101)):
> > > ^~~~
> >
> > IIRC GCC-8 fixed a bunch of -wrapv issues. Could be this is one of them
> > I suppose.
>
> Or better said, later GCCs returned back to the old behaviour of rejecting
> this only with -pedantic even in the presence of -fsanitize.

Only that it doesn't:

#define USB_ID(v,p) (((v)<<16)|(p))
void foo (unsigned int *i)
{
switch (*i)
{
case USB_ID(0xfc08, 0x0101):;
}
}

> gcc-11 -S t.c -std=c99 -fsanitize=shift
t.c: In function 'foo':
t.c:6:7: error: case label does not reduce to an integer constant
6 | case USB_ID(0xfc08, 0x0101):;
| ^~~~

for some reason it might fail to sanitize the case label for the
full testcase but clearly it doesn't so on purpose.

> But
> pedantically speaking (ahem!) it really isn't conforming c99 (which the
> compilation flags claim) , and in this case it seems easy enough to make
> the construct actually be conforming in the kernel sources, so that should
> perhaps be done?

Indeed. Simply cast vendor/product to (unsigned) in the USB_ID
macro to avoid arithmetic shifts.

Richard.