public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "slyfox at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/103292] [12 regression] xorg-server-1.20.13 -Werror=array-bounds false positive on unions
Date: Wed, 17 Nov 2021 21:49:06 +0000 [thread overview]
Message-ID: <bug-103292-4-GoD7vPut4D@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-103292-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103292
--- Comment #2 from Sergei Trofimovich <slyfox at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #1)
> The warning is intended. The program allocates an object of a size that's
> smaller than the size of the type used to access it:
>
> pPicture->pSourcePict = (union _SourcePict*) malloc(sizeof(struct
> _PictSolidFill));
> pPicture->pSourcePict->type = 0;
>
> It's not valid to access an object of one type using an lvalue of another.
> In simple cases GCC diagnoses violations of this requirement by
> -Wstrict-aliasing. -Warray-bounds doesn't detect aliasing violations but it
> does detect a subset of the problem that's apparent when the size of the
> lvalue's type is greater than the size of the object. The object must be
> big enough for the whole lvalue, even if the accessed member is within the
> bounds of the smaller object.
>
> The following is a smaller test case that should make the issue clearer.
> See also pr102151 for a similar report.
>
> $ cat a.c && gcc -O2 -S -Wall a.c
> struct A { char a[1]; };
> struct B { char a[2]; };
> union U { struct A a; struct B b; };
>
> void* f (void)
> {
> union U *p = __builtin_malloc (sizeof (struct A));
> p->a.a[0] = 0;
> return p;
> }
> a.c: In function ‘f’:
> a.c:8:4: warning: array subscript ‘union U[0]’ is partly outside array
> bounds of ‘unsigned char[1]’ [-Warray-bounds]
> 8 | p->a.a[0] = 0;
> | ^~
> a.c:7:16: note: object of size 1 allocated by ‘__builtin_malloc’
> 7 | union U *p = __builtin_malloc (sizeof (struct A));
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Aha, that makes sense. Filed upstream report as
https://gitlab.freedesktop.org/xorg/xserver/-/issues/1256
Related question:
It sounds like this diagnostic is somewhat related to -fstrict-aliasing. xorg
builds with -fno-strict-aliasing. Would it be fair to say the access in that
case is reasonable and -Warray-bounds is a false positive?
next prev parent reply other threads:[~2021-11-17 21:49 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-16 22:47 [Bug c/103292] New: " 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 [this message]
2021-11-17 22:34 ` [Bug middle-end/103292] " msebor at gcc dot gnu.org
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-103292-4-GoD7vPut4D@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).