public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Ilya Leoshkevich <iii@linux.ibm.com>
To: Mark Wielaard <mark@klomp.org>
Cc: elfutils-devel@sourceware.org, Ilya Leoshkevich <iii@linux.ibm.com>
Subject: [PATCH v2 1/7] libasm: Fix xdefault_pattern initialization
Date: Wed,  8 Feb 2023 20:52:20 +0100	[thread overview]
Message-ID: <20230208195226.144143-2-iii@linux.ibm.com> (raw)
In-Reply-To: <20230208195226.144143-1-iii@linux.ibm.com>

clang complains:

    asm_newscn.c:48:22: error: field 'pattern' with variable sized type 'struct FillPattern' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]
      struct FillPattern pattern;
                         ^

Fix by using a union instead. Define the second union member to be a
char array 1 byte larger than struct FillPattern. This should be legal
according to 6.7.9:

    If an object that has static or thread storage duration is not
    initialized explicitly, then ... if it is a union, the first named
    member is initialized (recursively) according to these rules, and
    any padding is initialized to zero bits.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
 libasm/asm_newscn.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/libasm/asm_newscn.c b/libasm/asm_newscn.c
index d258d969..f28c40f9 100644
--- a/libasm/asm_newscn.c
+++ b/libasm/asm_newscn.c
@@ -41,19 +41,25 @@
 
 
 /* Memory for the default pattern.  The type uses a flexible array
-   which does work well with a static initializer.  So we play some
-   dirty tricks here.  */
-static const struct
+   which does work well with a static initializer.  Work around this by
+   wrapping it in a union, whose second member is a char array 1 byte larger
+   than struct FillPattern.  According to 6.7.9, this does what we need:
+
+        If an object that has static or thread storage duration is not
+        initialized explicitly, then ... if it is a union, the first named
+        member is initialized (recursively) according to these rules, and
+        any padding is initialized to zero bits.  */
+
+static const union
 {
   struct FillPattern pattern;
-  char zero;
+  char zeroes[sizeof(struct FillPattern) + 1];
 } xdefault_pattern =
   {
     .pattern =
     {
       .len = 1
     },
-    .zero = '\0'
   };
 const struct FillPattern *__libasm_default_pattern = &xdefault_pattern.pattern;
 
-- 
2.39.1


  reply	other threads:[~2023-02-08 19:52 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-08 19:52 [PATCH v2 0/7] Add Memory Sanitizer support Ilya Leoshkevich
2023-02-08 19:52 ` Ilya Leoshkevich [this message]
2023-02-09 13:14   ` [PATCH v2 1/7] libasm: Fix xdefault_pattern initialization Mark Wielaard
2023-02-08 19:52 ` [PATCH v2 2/7] printversion: Fix unused variable Ilya Leoshkevich
2023-02-08 19:52 ` [PATCH v2 3/7] readelf: Fix set but not used parameter Ilya Leoshkevich
2023-02-09 13:17   ` Mark Wielaard
2023-02-08 19:52 ` [PATCH v2 4/7] x86_64_return_value_location: Support lvalue and rvalue references Ilya Leoshkevich
2023-02-09 13:45   ` Mark Wielaard
2023-02-10  1:20     ` Ilya Leoshkevich
2023-02-10 16:22       ` Frank Ch. Eigler
2023-02-08 19:52 ` [PATCH v2 5/7] configure: Use -fno-addrsig if possible Ilya Leoshkevich
2023-02-09 13:28   ` Mark Wielaard
2023-02-08 19:52 ` [PATCH v2 6/7] configure: Add --disable-demangler Ilya Leoshkevich
2023-02-09 13:40   ` Mark Wielaard
2023-02-08 19:52 ` [PATCH v2 7/7] configure: Add --enable-sanitize-memory Ilya Leoshkevich

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=20230208195226.144143-2-iii@linux.ibm.com \
    --to=iii@linux.ibm.com \
    --cc=elfutils-devel@sourceware.org \
    --cc=mark@klomp.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).