2022-04-06 07:59:10

by Richard Biener

[permalink] [raw]
Subject: Re: older gccs and case labels producing integer constants

On Tue, 5 Apr 2022, Richard Biener wrote:

> On Tue, 5 Apr 2022, Borislav Petkov wrote:
>
> > Hi folks,
> >
> > I'm starting to see failures like this on allmodconfig builds:
> >
> > 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)):
> > ^~~~
> >
> > (The case statement is a macro but it evaluates to what I have there)
> >
> > and that thing fails with
> >
> > $ gcc --version
> > gcc (SUSE Linux) 7.5.0
> >
> > although it doesn't have any problems building with newer compilers.
> >
> > I'm presuming older gccs consider those case statements signed ints and
> > the following fixes it:
> >
> > case ((((unsigned int)0xfc08) << 16) | (0x0101)):
> >
> > and I guess we can whack the couple of occurrences but what I'm
> > wondering is why does this work with newer gccs?
>
> I tried
>
> void foo (int i)
> {
> switch (i)
> {
> case (((0xfc08) << 16) | (0x0101)):;
> }
> }
>
> also with 'unsigned int i' but that's accepted with GCC 7. So
> what do you switch on?

Aha, also

> gcc-7 -S t.c -std=c11 -pedantic -pedantic-errors
t.c: In function 'foo':
t.c:6:7: error: case label is not an integer constant expression
[-Wpedantic]
case USB_ID(0xfc08, 0x0101):;
^~~~

aber _nur_ mit -std=c11 (oder c99, aber nicht c89) und -pedantic
-pedantic-errors.

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

Wird auch mit gcc 11 rejected. Kanns sein dass mit gcc 7 andere
compiler flags genommen werden?

--
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg,
Germany; GF: Ivo Totev; HRB 36809 (AG Nuernberg)


2022-04-06 13:35:17

by Borislav Petkov

[permalink] [raw]
Subject: Re: older gccs and case labels producing integer constants

On Tue, Apr 05, 2022 at 12:06:45PM +0200, Richard Biener wrote:
> Wird auch mit gcc 11 rejected. Kanns sein dass mit gcc 7 andere
> compiler flags genommen werden?

Found it:

$ gcc -fsanitize=shift -c switch.c
switch.c: In function ‘foo’:
switch.c:10:7: error: case label does not reduce to an integer constant
case (((0xfc08) << 16) | (0x0101)):;

$ gcc --version
gcc (SUSE Linux) 7.4.1 20190905 [gcc-7-branch revision 275407]
Copyright (C) 2017 Free Software Foundation, Inc.

Something not fully backported?

Thx.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2022-04-06 15:18:12

by Jakub Jelinek

[permalink] [raw]
Subject: Re: older gccs and case labels producing integer constants

On Tue, Apr 05, 2022 at 12:36:58PM +0200, Borislav Petkov wrote:
> On Tue, Apr 05, 2022 at 12:06:45PM +0200, Richard Biener wrote:
> > Wird auch mit gcc 11 rejected. Kanns sein dass mit gcc 7 andere
> > compiler flags genommen werden?
>
> Found it:
>
> $ gcc -fsanitize=shift -c switch.c
> switch.c: In function ‘foo’:
> switch.c:10:7: error: case label does not reduce to an integer constant
> case (((0xfc08) << 16) | (0x0101)):;
>
> $ gcc --version
> gcc (SUSE Linux) 7.4.1 20190905 [gcc-7-branch revision 275407]
> Copyright (C) 2017 Free Software Foundation, Inc.
>
> Something not fully backported?

That is rejected with -fsanitize=shift even on current trunk (in C, C++ is
fine).
C++ constexpr code has cases for ubsan builtins and internal functions,
but C just doesn't handle those apparently.

Jakub

2022-04-06 15:23:57

by Jakub Jelinek

[permalink] [raw]
Subject: Re: older gccs and case labels producing integer constants

On Wed, Apr 06, 2022 at 11:53:17AM +0200, Jakub Jelinek wrote:
> On Tue, Apr 05, 2022 at 12:36:58PM +0200, Borislav Petkov wrote:
> > On Tue, Apr 05, 2022 at 12:06:45PM +0200, Richard Biener wrote:
> > > Wird auch mit gcc 11 rejected. Kanns sein dass mit gcc 7 andere
> > > compiler flags genommen werden?
> >
> > Found it:
> >
> > $ gcc -fsanitize=shift -c switch.c
> > switch.c: In function ‘foo’:
> > switch.c:10:7: error: case label does not reduce to an integer constant
> > case (((0xfc08) << 16) | (0x0101)):;
> >
> > $ gcc --version
> > gcc (SUSE Linux) 7.4.1 20190905 [gcc-7-branch revision 275407]
> > Copyright (C) 2017 Free Software Foundation, Inc.
> >
> > Something not fully backported?
>
> That is rejected with -fsanitize=shift even on current trunk (in C, C++ is
> fine).
> C++ constexpr code has cases for ubsan builtins and internal functions,
> but C just doesn't handle those apparently.

But I think the error is actually correct.
In C99 and later, for signed left shift the rule for x << y is that
there is UB if (similarly to all C family) if y is negative or greater or
equal to precision of promoted x, but for C99 also when
((unsigned_typeof_x) x >> (precision_of_x - 1 - y)) != 0.
That is the case above, 0xfc08 is signed int and 0xfc08 << 16 is
0xfc080000 where (0xfc08 >> 15) is 1 and so it is UB.
In C99 and later you need:
case (int)(((0xfc08U) << 16) | (0x0101)):;
or so.
Note, C++ has different rules (and C++20 and later only has the
y non-negative and less than precision requirement and nothing else).

Jakub