public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug exp/17904] New: "Cannot access memory at address 0x0" for function returning reference evaluated within EVAL_AVOID_SIDE_EFFECTS
@ 2015-01-29 18:33 ed at catmur dot co.uk
  2015-01-30 13:40 ` [Bug exp/17904] " ed at catmur dot co.uk
  0 siblings, 1 reply; 2+ messages in thread
From: ed at catmur dot co.uk @ 2015-01-29 18:33 UTC (permalink / raw)
  To: gdb-prs

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

            Bug ID: 17904
           Summary: "Cannot access memory at address 0x0" for function
                    returning reference evaluated within
                    EVAL_AVOID_SIDE_EFFECTS
           Product: gdb
           Version: HEAD
            Status: NEW
          Severity: normal
          Priority: P2
         Component: exp
          Assignee: unassigned at sourceware dot org
          Reporter: ed at catmur dot co.uk

Given a function returning a reference:

    int& g() { static int i; return i; }

Evaluating the function within a EVAL_AVOID_SIDE_EFFECTS context gives an
error:

    (gdb) p 0 || +g()
    Cannot access memory at address 0x0

A workaround is to convert the reference to a pointer and indirect it:

    (gdb) p 0 || +*&g()
    $1 = true

This appears to be because OP_FUNCALL with EVAL_AVOID_SIDE_EFFECTS calls
allocate_value(), which for reference types returns a value with
location.address = 0; promotion on the reference argument then calls
coerce_ref() which gives a lazy value pointing to 0x0.

I'm not sure of a fix other than going through evaluate_subexp_standard()
putting in checks for noside == EVAL_AVOID_SIDE_EFFECTS.

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug exp/17904] "Cannot access memory at address 0x0" for function returning reference evaluated within EVAL_AVOID_SIDE_EFFECTS
  2015-01-29 18:33 [Bug exp/17904] New: "Cannot access memory at address 0x0" for function returning reference evaluated within EVAL_AVOID_SIDE_EFFECTS ed at catmur dot co.uk
@ 2015-01-30 13:40 ` ed at catmur dot co.uk
  0 siblings, 0 replies; 2+ messages in thread
From: ed at catmur dot co.uk @ 2015-01-30 13:40 UTC (permalink / raw)
  To: gdb-prs

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

--- Comment #1 from Ed Catmur <ed at catmur dot co.uk> ---
Here's a test:

gdb/testsuite/gdb.cp/pr17904.cc

int&
func ()
{
  static int i;
  return i;
}

int
main ()
{
}

gdb/testsuite/gdb.cp/pr17904.exp

if {[skip_cplus_tests]} { continue }

standard_testfile .cc

if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}]} {
    return -1
}

if {![runto_main]} {
    return -1
}

gdb_test "p 0 || +func()" ".* = false" "reference return"

And a suggested patch (works, but I'm not too sure about correctness):

--- a/gdb/value.c
+++ b/gdb/value.c
@@ -3650,6 +3650,7 @@ coerce_ref (struct value *arg)
 {
   struct type *value_type_arg_tmp = check_typedef (value_type (arg));
   struct value *retval;
+  CORE_ADDR addr;
   struct type *enc_type;

   retval = coerce_ref_if_computed (arg);
@@ -3662,9 +3663,12 @@ coerce_ref (struct value *arg)
   enc_type = check_typedef (value_enclosing_type (arg));
   enc_type = TYPE_TARGET_TYPE (enc_type);

-  retval = value_at_lazy (enc_type,
-                          unpack_pointer (value_type (arg),
-                                          value_contents (arg)));
+  addr = unpack_pointer (value_type (arg), value_contents (arg));
+
+  if (VALUE_LVAL (arg) == not_lval && addr == 0)
+    return allocate_value (enc_type);
+
+  retval = value_at_lazy (enc_type, addr);
   enc_type = value_type (retval);
   return readjust_indirect_value_type (retval, enc_type,
                                        value_type_arg_tmp, arg);

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-01-30 13:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-29 18:33 [Bug exp/17904] New: "Cannot access memory at address 0x0" for function returning reference evaluated within EVAL_AVOID_SIDE_EFFECTS ed at catmur dot co.uk
2015-01-30 13:40 ` [Bug exp/17904] " ed at catmur dot co.uk

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).