[RFA,7/8] Use unique_xmalloc_ptr in execute_gdb_command

Message ID 1480395946-10924-8-git-send-email-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Nov. 29, 2016, 5:05 a.m. UTC
  This replaces a cleanup in execute_gdb_command with an instance of
unique_xmalloc_ptr.  std::string was not used because execute_command
and execute_command_to_string don't accept a "const char *" (in fact
the reason for copying the string at all).

2016-11-28  Tom Tromey  <tom@tromey.com>

	* python/python.c (execute_gdb_command): Use unique_xmalloc_ptr.
---
 gdb/ChangeLog       | 4 ++++
 gdb/python/python.c | 8 +++-----
 2 files changed, 7 insertions(+), 5 deletions(-)
  

Comments

Tom Tromey Nov. 29, 2016, 5:22 a.m. UTC | #1
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> This replaces a cleanup in execute_gdb_command with an instance of
Tom> unique_xmalloc_ptr.  std::string was not used because execute_command
Tom> and execute_command_to_string don't accept a "const char *" (in fact
Tom> the reason for copying the string at all).

To my surprise, this patch is broken.
I must not have re-run the python tests locally after writing it :(

The problem is that prevent_dont_repeat returns a cleanup, which is then
left dangling after this patch.

I'm looking into a fix.  Meanwhile I think the rest of the series can
still be considered.

Tom
  
Pedro Alves Dec. 2, 2016, 2:49 p.m. UTC | #2
On 11/29/2016 05:05 AM, Tom Tromey wrote:
> This replaces a cleanup in execute_gdb_command with an instance of
> unique_xmalloc_ptr.  std::string was not used because execute_command
> and execute_command_to_string don't accept a "const char *" (in fact
> the reason for copying the string at all).

You can do:

 std::string copy (arg);
 ...
 to_string_res = execute_command_to_string (&copy[0], from_tty);

That's fine in C++11.

Thanks,
Pedro Alves
  
Tom Tromey Dec. 13, 2016, 1:30 p.m. UTC | #3
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> You can do:
Pedro>  std::string copy (arg);
Pedro>  ...
Pedro>  to_string_res = execute_command_to_string (&copy[0], from_tty);

Thanks, I made this change.

Tom
  

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index cf61306..d0d2ef6 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,9 @@ 
 2016-11-28  Tom Tromey  <tom@tromey.com>
 
+	* python/python.c (execute_gdb_command): Use unique_xmalloc_ptr.
+
+2016-11-28  Tom Tromey  <tom@tromey.com>
+
 	* value.h (value_freer::~value_freer): Call release.
 	(value_freer::release): New method.
 	* dwarf2loc.c (dwarf2_evaluate_loc_desc_full): Use value_freer.
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 83b9805..83894a1 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -601,8 +601,7 @@  execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
   TRY
     {
       /* Copy the argument text in case the command modifies it.  */
-      char *copy = xstrdup (arg);
-      struct cleanup *cleanup = make_cleanup (xfree, copy);
+      gdb::unique_xmalloc_ptr<char> copy (xstrdup (arg));
       struct interp *interp;
 
       scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
@@ -616,10 +615,9 @@  execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
 
       prevent_dont_repeat ();
       if (to_string)
-	to_string_res = execute_command_to_string (copy, from_tty);
+	to_string_res = execute_command_to_string (copy.get (), from_tty);
       else
-	execute_command (copy, from_tty);
-      do_cleanups (cleanup);
+	execute_command (copy.get (), from_tty);
     }
   CATCH (except, RETURN_MASK_ALL)
     {