public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/104205] New: Signed overflow in case label reports confusing warning with -Wpedantic
@ 2022-01-24 15:00 stephen.wassell at sophos dot com
  2022-01-24 17:23 ` [Bug c/104205] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: stephen.wassell at sophos dot com @ 2022-01-24 15:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104205

            Bug ID: 104205
           Summary: Signed overflow in case label reports confusing
                    warning with -Wpedantic
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stephen.wassell at sophos dot com
  Target Milestone: ---

When compiling the following code with "gcc -Wpedantic":

    int convert(int err)
    {
        switch (err)
        {
            case 1 << 31: // signed overflow - undefined behaviour
                return 0;
            default:
                return 1;
        }
    }

    int main()
    {
        return convert(1 << 31);
    }

It reports a confusingly worded warning since version 6. It should say
something about undefined behaviour instead. Should there also be a warning for
the other use of 1 << 31?

    <source>: In function 'convert':
    <source>:5:9: warning: case label is not an integer constant expression
[-Wpedantic]
    5 |         case 1 << 31:
      |         ^~~~

Seen on gcc (Ubuntu 11.2.0-7ubuntu2) 11.2.0 x64, and multiple versions on
Compiler Explorer.

https://godbolt.org/z/4Tr1qxaqv

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/104205] Signed overflow in case label reports confusing warning with -Wpedantic
  2022-01-24 15:00 [Bug c/104205] New: Signed overflow in case label reports confusing warning with -Wpedantic stephen.wassell at sophos dot com
@ 2022-01-24 17:23 ` pinskia at gcc dot gnu.org
  2022-01-24 17:28 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-24 17:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104205

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                URL|https://godbolt.org/z/4Tr1q |
                   |xaqv                        |
           Keywords|                            |diagnostic

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://godbolt.org/z/4Tr1qxaqv

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/104205] Signed overflow in case label reports confusing warning with -Wpedantic
  2022-01-24 15:00 [Bug c/104205] New: Signed overflow in case label reports confusing warning with -Wpedantic stephen.wassell at sophos dot com
  2022-01-24 17:23 ` [Bug c/104205] " pinskia at gcc dot gnu.org
@ 2022-01-24 17:28 ` pinskia at gcc dot gnu.org
  2022-01-24 17:37 ` stephen.wassell at sophos dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-24 17:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104205

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>  It should say something about undefined behaviour instead.

The problem is for integer constants, it is not undefined at runtime but rather
invalid code at compile time.

>Should there also be a warning for the other use of 1 << 31?

I think you misunderstood the warning/error fully.

In C99-C17, 1<<31 is not an integer constant expression as there is an
overflow. 

But it is still valid, just undefined at runtime if used where an integer
constant expression is not required. case statements require an integer
constant expression.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/104205] Signed overflow in case label reports confusing warning with -Wpedantic
  2022-01-24 15:00 [Bug c/104205] New: Signed overflow in case label reports confusing warning with -Wpedantic stephen.wassell at sophos dot com
  2022-01-24 17:23 ` [Bug c/104205] " pinskia at gcc dot gnu.org
  2022-01-24 17:28 ` pinskia at gcc dot gnu.org
@ 2022-01-24 17:37 ` stephen.wassell at sophos dot com
  2022-01-24 20:07 ` egallager at gcc dot gnu.org
  2022-01-24 22:46 ` joseph at codesourcery dot com
  4 siblings, 0 replies; 6+ messages in thread
From: stephen.wassell at sophos dot com @ 2022-01-24 17:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104205

--- Comment #3 from Stephen Wassell <stephen.wassell at sophos dot com> ---
Thank you for the response! I think the cause of our confusion was that we were
focusing on "constant" in the warning message rather than "integer". Our
original code had a few layers of macros so the signed overflow wasn't obvious
at first.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/104205] Signed overflow in case label reports confusing warning with -Wpedantic
  2022-01-24 15:00 [Bug c/104205] New: Signed overflow in case label reports confusing warning with -Wpedantic stephen.wassell at sophos dot com
                   ` (2 preceding siblings ...)
  2022-01-24 17:37 ` stephen.wassell at sophos dot com
@ 2022-01-24 20:07 ` egallager at gcc dot gnu.org
  2022-01-24 22:46 ` joseph at codesourcery dot com
  4 siblings, 0 replies; 6+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-01-24 20:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104205

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #4 from Eric Gallager <egallager at gcc dot gnu.org> ---
Maybe the warning text could be updated to include what's actually being used
for the case label after macro expansion etc.? e.g.:
<source>:5:9: warning: case label '1 << 31' is not an integer constant
expression [-Wpedantic]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/104205] Signed overflow in case label reports confusing warning with -Wpedantic
  2022-01-24 15:00 [Bug c/104205] New: Signed overflow in case label reports confusing warning with -Wpedantic stephen.wassell at sophos dot com
                   ` (3 preceding siblings ...)
  2022-01-24 20:07 ` egallager at gcc dot gnu.org
@ 2022-01-24 22:46 ` joseph at codesourcery dot com
  4 siblings, 0 replies; 6+ messages in thread
From: joseph at codesourcery dot com @ 2022-01-24 22:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104205

--- Comment #5 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
Printing an expression within the diagnostic message is generally 
problematic, but it might be good to change the caret location to point to 
the expression rather than the keyword "case" (or to a range covering both 
the keyword and the expression).

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-01-24 22:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-24 15:00 [Bug c/104205] New: Signed overflow in case label reports confusing warning with -Wpedantic stephen.wassell at sophos dot com
2022-01-24 17:23 ` [Bug c/104205] " pinskia at gcc dot gnu.org
2022-01-24 17:28 ` pinskia at gcc dot gnu.org
2022-01-24 17:37 ` stephen.wassell at sophos dot com
2022-01-24 20:07 ` egallager at gcc dot gnu.org
2022-01-24 22:46 ` joseph at codesourcery dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).