public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "muecker at gwdg dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/109956] GCC reserves 9 bytes for struct s { int a; char b; char t[]; } x = {1, 2, 3};
Date: Thu, 25 May 2023 16:42:33 +0000	[thread overview]
Message-ID: <bug-109956-4-5AipO26HdF@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-109956-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #12 from Martin Uecker <muecker at gwdg dot de> ---

The C standard says "However, when a . (or -> ) operator has a left operand
that is (a pointer to) a structure with a flexible array member and the right
operand names that member, it behaves as if that member were replaced with the
longest array (with the same element type) that would not make the structure
larger than the object being accessed;" 

This would imply that also for GCC not all elements can be accessed in the
following structure without invoking UB. For x86_64 'x' has 11 bytes (for clang
9) but a struct with replacement array actually needs 12.

struct foo { int a; short b; char t[]; } x = { .t = { 1, 2, 3 } }; 
// x has 11 bytes

struct bar { int a; short b; char t[3]; }; // 12 bytes


One can argue that this does not matter for an extension and maybe the C
standard should not be read in this way as itself also contains an example
using the sizeof() + n * sizeof() rule which implies that this amount of
storage is enough for n elements.

But the question is what programmers should use when using memcpy of statically
initialized structs which have a FAM?   sizeof() + n * sizeof() works for GCC
but not for clang.  sizeof(struct bar) works for neither.  One can use
MAX(sizeof(struct foo, offsetof(struct foo, t) + n * sizeof(char)) but I have
not seen anybody use it.

  parent reply	other threads:[~2023-05-25 16:42 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-24 16:09 [Bug c/109956] New: " pascal_cuoq at hotmail dot com
2023-05-24 16:20 ` [Bug c/109956] " pinskia at gcc dot gnu.org
2023-05-24 16:28 ` muecker at gwdg dot de
2023-05-24 18:30 ` pascal_cuoq at hotmail dot com
2023-05-24 19:18 ` muecker at gwdg dot de
2023-05-24 20:25 ` muecker at gwdg dot de
2023-05-24 21:50 ` joseph at codesourcery dot com
2023-05-24 21:55 ` joseph at codesourcery dot com
2023-05-25  5:23 ` amonakov at gcc dot gnu.org
2023-05-25  5:29 ` muecker at gwdg dot de
2023-05-25  5:30 ` pinskia at gcc dot gnu.org
2023-05-25  7:53 ` rguenth at gcc dot gnu.org
2023-05-25 16:42 ` muecker at gwdg dot de [this message]
2023-05-26 12:03 ` pascal_cuoq at hotmail dot com
2023-05-26 16:24 ` muecker at gwdg dot de
2023-08-08 14:57 ` muecker at gwdg dot de
2024-03-14 20:17 ` pinskia 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-109956-4-5AipO26HdF@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).