public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/103292] New: [12 regression] xorg-server-1.20.13 -Werror=array-bounds false positive on unions
@ 2021-11-16 22:47 slyfox at gcc dot gnu.org
  2021-11-17  1:32 ` [Bug c/103292] " msebor at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: slyfox at gcc dot gnu.org @ 2021-11-16 22:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103292
           Summary: [12 regression] xorg-server-1.20.13
                    -Werror=array-bounds false positive on unions
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: slyfox at gcc dot gnu.org
  Target Milestone: ---

Created attachment 51818
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51818&action=edit
a.c.c.orig

Initially observed build failure on xorg-server-1.20.13.

Looks like gcc detects out-of-bounds access on union of structs of different
sizes. Extracted example from unreduced a.c.c.orig (attached):

$ cat a.c.c
    typedef long unsigned int size_t;
    extern void *malloc (size_t __size) __attribute__ ((__nothrow__ ,
__leaf__)) __attribute__ ((__malloc__)) __attribute__ ((__alloc_size__ (1)))
__attribute__ ((__warn_unused_result__));

    struct _PictSolidFill {
        unsigned int type;

        char foo[20];
    };

    struct _PictHuge {
        unsigned int type;

        char foo[200];
    };

    union _SourcePict {
        // each union member has a type
        unsigned int type;

        struct _PictSolidFill maybePSF;

        // presence of this field triggers an error
        struct _PictHuge maybeHuge;
    };

    struct _Picture {
        union _SourcePict* pSourcePict;
    };

    extern
    void CreateSolidPicture(struct _Picture* pPicture);
    void CreateSolidPicture(struct _Picture* pPicture)
    {
        pPicture->pSourcePict = (union _SourcePict*) malloc(sizeof(struct
_PictSolidFill));
        pPicture->pSourcePict->type = 0;
    }

$ gcc-12.0.0 -Werror=array-bounds -c a.c.c -O2
a.c.c: In function 'CreateSolidPicture':
a.c.c:47:30: error: array subscript 'union _SourcePict[0]' is partly outside
array bounds of 'unsigned char[24]' [-Werror=array-bounds]
   47 |         pPicture->pSourcePict->type = 0;
      |                              ^~
a.c.c:46:54: note: object of size 24 allocated by 'malloc'
   46 |         pPicture->pSourcePict = (union _SourcePict*)
malloc(sizeof(struct _PictSolidFill));
      |                                                     
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

$ gcc-12.0.0 -v
Using built-in specs.
COLLECT_GCC=/nix/store/59jdmdy3ylrpmap1bjxic1fjaq8wf96s-gcc-12.0.0/bin/gcc
COLLECT_LTO_WRAPPER=/nix/store/59jdmdy3ylrpmap1bjxic1fjaq8wf96s-gcc-12.0.0/libexec/gcc/x86_64-unknown-linux-gnu/12.0.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with:
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.0.0 20211114 (experimental) (GCC)

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

end of thread, other threads:[~2021-11-17 22:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-16 22:47 [Bug c/103292] New: [12 regression] xorg-server-1.20.13 -Werror=array-bounds false positive on unions slyfox at gcc dot gnu.org
2021-11-17  1:32 ` [Bug c/103292] " msebor at gcc dot gnu.org
2021-11-17 21:49 ` slyfox at gcc dot gnu.org
2021-11-17 22:34 ` [Bug middle-end/103292] " msebor at gcc dot gnu.org

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).