[RFA] Change rs6000_ptrace_ldinfo to return a byte_vector

Message ID 20180401162519.14801-1-tom@tromey.com
State New
Headers show
Series
  • [RFA] Change rs6000_ptrace_ldinfo to return a byte_vector
Related show

Commit Message

Tom Tromey April 1, 2018, 4:25 p.m.
This changes rs6000_ptrace_ldinfo to return a byte_vector.  I think
this points out an existing double-free in
rs6000_xfer_shared_libraries.

Tested by the buildbot.

gdb/ChangeLog
2018-03-31  Tom Tromey  <tom@tromey.com>

	* rs6000-nat.c (rs6000_ptrace_ldinfo): Return a byte_vector.
	(rs6000_xfer_shared_libraries): Update.
---
 gdb/ChangeLog    |  5 +++++
 gdb/rs6000-nat.c | 28 ++++++++++------------------
 2 files changed, 15 insertions(+), 18 deletions(-)

-- 
2.13.6

Comments

Simon Marchi April 1, 2018, 6:29 p.m. | #1
On 2018-04-01 12:25 PM, Tom Tromey wrote:
> This changes rs6000_ptrace_ldinfo to return a byte_vector.  I think

> this points out an existing double-free in

> rs6000_xfer_shared_libraries.


Eh, indeed.

> Tested by the buildbot.

> 

> gdb/ChangeLog

> 2018-03-31  Tom Tromey  <tom@tromey.com>

> 

> 	* rs6000-nat.c (rs6000_ptrace_ldinfo): Return a byte_vector.

> 	(rs6000_xfer_shared_libraries): Update.

> ---

>  gdb/ChangeLog    |  5 +++++

>  gdb/rs6000-nat.c | 28 ++++++++++------------------

>  2 files changed, 15 insertions(+), 18 deletions(-)

> 

> diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c

> index 4f05cd6b6a..fbc9b52623 100644

> --- a/gdb/rs6000-nat.c

> +++ b/gdb/rs6000-nat.c

> @@ -587,21 +587,21 @@ rs6000_create_inferior (struct target_ops * ops, const char *exec_file,

>  

>     The returned value must be deallocated after use.  */


This comment here could be removed.

Otherwise, LGTM.

Simon

Patch

diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
index 4f05cd6b6a..fbc9b52623 100644
--- a/gdb/rs6000-nat.c
+++ b/gdb/rs6000-nat.c
@@ -587,21 +587,21 @@  rs6000_create_inferior (struct target_ops * ops, const char *exec_file,
 
    The returned value must be deallocated after use.  */
 
-static gdb_byte *
+static gdb::byte_vector
 rs6000_ptrace_ldinfo (ptid_t ptid)
 {
   const int pid = ptid_get_pid (ptid);
-  int ldi_size = 1024;
-  void *ldi = xmalloc (ldi_size);
+  gdb::byte_vector ldi (1024);
   int rc = -1;
 
   while (1)
     {
       if (ARCH64 ())
-	rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, ldi_size,
-			      NULL);
+	rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi.data (),
+			      ldi.size (), NULL);
       else
-	rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi, ldi_size, NULL);
+	rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi.data (),
+			      ldi.size (), NULL);
 
       if (rc != -1)
 	break; /* Success, we got the entire ld_info data.  */
@@ -610,11 +610,10 @@  rs6000_ptrace_ldinfo (ptid_t ptid)
 	perror_with_name (_("ptrace ldinfo"));
 
       /* ldi is not big enough.  Double it and try again.  */
-      ldi_size *= 2;
-      ldi = xrealloc (ldi, ldi_size);
+      ldi.resize (ldi.size () * 2);
     }
 
-  return (gdb_byte *) ldi;
+  return ldi;
 }
 
 /* Implement the to_xfer_partial target_ops method for
@@ -626,9 +625,7 @@  rs6000_xfer_shared_libraries
    const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
    ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
 {
-  gdb_byte *ldi_buf;
   ULONGEST result;
-  struct cleanup *cleanup;
 
   /* This function assumes that it is being run with a live process.
      Core files are handled via gdbarch.  */
@@ -637,14 +634,9 @@  rs6000_xfer_shared_libraries
   if (writebuf)
     return TARGET_XFER_E_IO;
 
-  ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
-  gdb_assert (ldi_buf != NULL);
-  cleanup = make_cleanup (xfree, ldi_buf);
-  result = rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf,
+  gdb::byte_vector ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
+  result = rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf.data (),
 				      readbuf, offset, len, 1);
-  xfree (ldi_buf);
-
-  do_cleanups (cleanup);
 
   if (result == 0)
     return TARGET_XFER_EOF;