public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "vries at gcc dot gnu.org" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug exp/26875] Incorrect value printed for address of first element of zero-length array
Date: Fri, 13 Nov 2020 14:47:00 +0000	[thread overview]
Message-ID: <bug-26875-4717-yCPl4vnV4o@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-26875-4717@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=26875

--- Comment #3 from Tom de Vries <vries at gcc dot gnu.org> ---
This passes testing:
...
diff --git a/gdb/eval.c b/gdb/eval.c
index 8d0c5747f39..0e8698bd488 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -2814,6 +2814,39 @@ evaluate_subexp_for_address (struct expression *exp, int
*pos,


   switch (op)
     {
+    case BINOP_SUBSCRIPT:
+      if (noside != EVAL_SKIP)
+       {
+         /* Skip over the base to get the position of the index.  */
+         int pos_index = *pos + 1;
+         evaluate_subexp (nullptr, exp, &pos_index, EVAL_SKIP);
+
+         /* Check if the index is zero.  Do this without evaluating
+            side effects, such that we can still fall back to default_case. 
*/
+         struct value *index
+           = evaluate_subexp (nullptr, exp, &pos_index,
+                              EVAL_AVOID_SIDE_EFFECTS);
+         if (value_equal (index, value_zero (value_type (index), not_lval)))
+           {
+             /* We're handling op here, skip it.  */
+             (*pos)++;
+
+             /* Evaluate base.  */
+             struct value *base = evaluate_subexp (nullptr, exp, pos, noside);
+
+             /* Evaluate index, for the side-effect, and to return with pos
+                past index.  */
+             evaluate_subexp (nullptr, exp, pos, noside);
+
+             /* Translate &base[0] into (typeof(base[0]) *)base.   */
+             type *element_type = TYPE_TARGET_TYPE (value_type (base));
+             type *return_type = lookup_pointer_type (element_type);
+             return value_cast (return_type, base);
+           }
+       }
+
+      goto default_case;
+
     case UNOP_IND:
       (*pos)++;
       x = evaluate_subexp (nullptr, exp, pos, noside);
...

-- 
You are receiving this mail because:
You are on the CC list for the bug.

  parent reply	other threads:[~2020-11-13 14:47 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-13  9:32 [Bug exp/26875] New: " vries at gcc dot gnu.org
2020-11-13 10:33 ` [Bug exp/26875] " vries at gcc dot gnu.org
2020-11-13 10:53 ` vries at gcc dot gnu.org
2020-11-13 14:47 ` vries at gcc dot gnu.org [this message]
2020-11-13 16:52 ` vries at gcc dot gnu.org
2020-11-14 11:45 ` vries at gcc dot gnu.org
2020-11-14 12:04 ` vries at gcc dot gnu.org
2020-11-20 18:00 ` simark at simark dot ca
2020-11-20 18:03 ` simark at simark dot ca
2020-11-20 18:07 ` simark at simark dot ca
2020-11-20 18:17 ` simark at simark dot ca
2020-11-20 22:16 ` vries at gcc dot gnu.org
2020-11-20 22:25 ` simark at simark dot ca
2020-11-23 16:25 ` simark at simark dot ca
2020-12-09 18:53 ` cvs-commit at gcc dot gnu.org
2020-12-09 21:34 ` cvs-commit at gcc dot gnu.org
2020-12-09 22:11 ` simark at simark dot ca
2021-10-13 14:22 ` vries 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-26875-4717-yCPl4vnV4o@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@sourceware.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).