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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|NEW                         |RESOLVED

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
By using a larger type to access a smaller object the code violates the strict
aliasing rule, and this instance of the warning for it is intended.  The
problem can be reduced to the following test case where GCC points out the
aliasing violation by -Wincompatible-pointer-types, besides also issuing
-Warray-bounds.

The warning could stand to be rephrased to make the problem clearer and moved
under the -Wstrict-alising option (which would also avoid it in code bases like
the Linux kernel that use -fno-strict-aliasing).  I submitted a patch to do all
that last March in an effort to resolve pr98503 of which this effectively a
duplicate, but the reviewer declined to approve it.

$ cat pr104341.c && gcc -O2 -S -Wall pr104341.c
int acpi_sx_zsdt_acpi;

void f (void)
{
  union {
    int u32[2];
  } *d = &acpi_sx_zsdt_acpi;

  d->u32[0] = 0;
}
pr104341.c: In function ‘f’:
pr104341.c:7:10: warning: initialization of ‘union <anonymous> *’ from
incompatible pointer type ‘int *’ [-Wincompatible-pointer-types]
    7 |   } *d = &acpi_sx_zsdt_acpi;
      |          ^
pr104341.c:9:4: warning: array subscript ‘union <anonymous>[0]’ is partly
outside array bounds of ‘int[1]’ [-Warray-bounds]
    9 |   d->u32[0] = 0;
      |    ^~
pr104341.c:1:5: note: object ‘acpi_sx_zsdt_acpi’ of size 4
    1 | int acpi_sx_zsdt_acpi;
      |     ^~~~~~~~~~~~~~~~~

*** This bug has been marked as a duplicate of bug 98503 ***

Reply via email to