[RFA,7/8] Use unique_xmalloc_ptr in execute_gdb_command
Commit Message
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" == 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
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 (©[0], from_tty);
That's fine in C++11.
Thanks,
Pedro Alves
>>>>> "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 (©[0], from_tty);
Thanks, I made this change.
Tom
@@ -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.
@@ -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 (¤t_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)
{