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 via email to