public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "burnus at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/112479] New: Missing -Woverflow warnings with bit fields with assignment of a constant
Date: Fri, 10 Nov 2023 18:41:03 +0000	[thread overview]
Message-ID: <bug-112479-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 112479
           Summary: Missing -Woverflow warnings with bit fields with
                    assignment of a constant
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
  Target Milestone: ---

I noticed that -Woverflow misses some cases of -Woverflow.

DISCLAIMER: It might be that some sign/unsigned promotion makes it desirably
that no warning is shown. Still, as a mere user, I find it odd to see no
warning.

* * *

I did notice that Clang's -Wbitfield-constant-conversion warning (also on by
default) shows a rather similar pattern of warning vs. no warning. However,
Clang does warn for the following two assignments:

  s.b = 2; // MISSING diagnostic for 2 -> -2
  s.b = 3; // MISSING diagnostic for 3 -> -1

Thus, there is at least some inconsistency with regards to the warning between
GCC and Clang

* * *

/* Either of the following warning is shown by GCC, if any:
warning: overflow in conversion from ‘int’ to ‘signed char:1’ changes value
from ‘1’ to ‘-0’ [-Woverflow]
warning: unsigned conversion from ‘int’ to ‘unsigned char:1’ changes value from
‘-3’ to ‘1’ [-Woverflow]
*/

void
my_signed()
{
  struct { int a:1, b:2; } s;
  s.a = -3; // Diagnosed: from ‘-3’ to ‘-1’
  s.a = -2; // Diagnosed: from ‘-2’ to ‘0’
  s.a = -1; // valid
  s.a = 0; // valid
  s.a = 1; // MISSING diagnostic for 1 -> -1
  s.a = 2; // Diagnosed: from ‘2’ to ‘0’
  s.a = 3; // Diagnosed: from ‘3’ to ‘-1’

  s.b = -3; // Diagnosed: from ‘-3’ to ‘1’
  s.b = -2; // valid
  s.b = -1; // valid
  s.b = 0; // valid
  s.b = 1; // valid
  s.b = 2; // MISSING diagnostic for 2 -> -2
  s.b = 3; // MISSING diagnostic for 3 -> -1
  s.b = 4; // Diagnosed: from ‘4’ to ‘0’
}

void
my_unsigned()
{
  struct { unsigned a:1, b:2; } u;
  u.a = -3; // Diagnosed: from ‘-3’ to ‘1’
  u.a = -2; // Diagnosed: from ‘-2’ to ‘0’
  u.a = -1; // MISSING diagnostic for -1 -> 1
  u.a = 0; // valid
  u.a = 1; // valid
  u.a = 2; // Diagnosed: from ‘2’ to ‘0’
  u.a = 3; // Diagnosed: from ‘3’ to ‘1’

  u.b = -3; // Diagnosed: from ‘-3’ to ‘1’
  u.b = -2; // MISSING diagnostic for -2 -> 2
  u.b = -1; // MISSING diagnostic for -1 -> 3
  u.b = 0; // valid
  u.b = 1; // valid
  u.b = 2; // valid
  u.b = 3; // valid
  u.b = 4; // Diagnosed: from ‘4’ to ‘0’
}

                 reply	other threads:[~2023-11-10 18:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-112479-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).