linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Keeping <john@keeping.me.uk>
To: Linux-Sparse <linux-sparse@vger.kernel.org>
Cc: John Keeping <john@keeping.me.uk>
Subject: [PATCH] Fix offsets in anonymous unions
Date: Thu, 22 May 2014 14:22:57 +0100	[thread overview]
Message-ID: <f6d65cc35d3627885e7a1c89e70a48c3368d4f66.1400764815.git.john@keeping.me.uk> (raw)

Commit 38d1124 (Fix error at anoymous unions, 2014-04-03) says:

    Ok, this fixes the warning, but we seem to still mess up the actual
    initializer. It looks like some later phase gets the offset wrong, so
    when we lay things out in memory, we'll put things at offset zero
    (which is right for your test-case, but not if there was something
    before that anonymous union).

This is because in evaluate.c::convert_ident() the offset of a member of
the inner union is set to the offset within the union, not the overall
offset.

Earlier, the correct offset has been calculated for check_designators()
by find_identifier() so by setting the offset there we end up using the
correct offset later on.

Signed-off-by: John Keeping <john@keeping.me.uk>
---
I'm pretty sure this is the wrong fix and I suspect it breaks some other
scenarios like mixing designators and plain initializers, but I've run
out of time to investigate this further and hopefully the correct patch
will be obvious to someone who knows the code better...

 evaluate.c              | 3 +--
 validation/anon-union.c | 3 ++-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/evaluate.c b/evaluate.c
index 7ce8c55..cb58ccc 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -2223,9 +2223,7 @@ static void convert_index(struct expression *e)
 static void convert_ident(struct expression *e)
 {
 	struct expression *child = e->ident_expression;
-	struct symbol *sym = e->field;
 	e->type = EXPR_POS;
-	e->init_offset = sym->offset;
 	e->init_nr = 1;
 	e->init_expr = child;
 }
@@ -2328,6 +2326,7 @@ static struct expression *check_designators(struct expression *e,
 				break;
 			}
 			e->field = e->ctype = ctype;
+			e->init_offset = offset;
 			last = e;
 			if (!e->ident_expression) {
 				err = "invalid";
diff --git a/validation/anon-union.c b/validation/anon-union.c
index 487f957..8670868 100644
--- a/validation/anon-union.c
+++ b/validation/anon-union.c
@@ -1,10 +1,11 @@
 struct s {
+        int type;
         union {
                 int val;
         };
 };
 
-static struct s foo = { .val = 5, };
+static struct s foo = { .type = 42, .val = 5, };
 /*
  * check-name: test anonymous union initializer
  */
-- 
2.0.0.rc2.4.g1dc51c6


             reply	other threads:[~2014-05-22 13:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-22 13:22 John Keeping [this message]
2014-05-23 18:40 ` [PATCH] Fix offsets in anonymous unions Christopher Li
2014-06-01  7:55 ` Christopher Li

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=f6d65cc35d3627885e7a1c89e70a48c3368d4f66.1400764815.git.john@keeping.me.uk \
    --to=john@keeping.me.uk \
    --cc=linux-sparse@vger.kernel.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 NNTP newsgroup(s).