From 9b9720149dfee4a9a961c29d0382fc5bdf9c975b Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sat, 15 Oct 2016 09:20:02 -0600 Subject: Use unique_xmalloc_ptr in Python code This changes some utility functions in the Python code to return unique_xmalloc_ptr, and then fixes up the callers. I chose unique_xmalloc_ptr rather than std::string because at a few call points the xmalloc'd string is released and ownership transferred elsewhere. This patch found a few existing memory leaks. For example, py-unwind.c called gdbpy_obj_to_string but never freed the result. Built and regression tested on the buildbot. 2016-11-09 Tom Tromey * varobj.h (varobj_get_display_hint): Change return type. * varobj.c (varobj_get_display_hint): Return unique_xmalloc_ptr. (varobj_value_get_print_value): Update. * python/python.c (gdbpy_before_prompt_hook, gdbpy_print_stack) (gdbpy_apply_type_printers): Update. * python/python-internal.h (unicode_to_target_string) (python_string_to_target_string, python_string_to_host_string) (gdbpy_obj_to_string, gdbpy_exception_to_string) (gdbpy_get_display_hint): Change return types. * python/py-varobj.c (py_varobj_iter_next): Update. * python/py-value.c (valpy_getitem, convert_value_from_python): Update. * python/py-utils.c (unicode_to_encoded_string) (unicode_to_target_string, python_string_to_target_string) (python_string_to_host_string, gdbpy_obj_to_string) (gdbpy_exception_to_string): Return unique_xmalloc_ptr. * python/py-unwind.c (pyuw_parse_register_id): Update. * python/py-type.c (typy_getitem): Update. * python/py-prettyprint.c (gdbpy_get_display_hint) (print_stack_unless_memory_error, print_children) (gdbpy_apply_val_pretty_printer): Update. * python/py-param.c (set_parameter_value): Update. (get_doc_string, call_doc_function): Return unique_xmalloc_ptr. (get_set_value, get_show_value, compute_enum_values, parmpy_init): Update. * python/py-infthread.c (thpy_set_name): Update. * python/py-function.c (fnpy_call, fnpy_init): Update. * python/py-framefilter.c (extract_sym): Change "name" to unique_xmalloc_ptr. (enumerate_args, enumerate_locals): Update. (py_print_frame): Use unique_xmalloc_ptr. * python/py-frame.c (frapy_read_var): Update. Remove cleanup. * python/py-cmd.c (cmdpy_function, cmdpy_completer, cmdpy_init): Update. * python/py-breakpoint.c (bppy_set_condition): Use unique_xmalloc_ptr. (bppy_init): Likewise. Remove cleanup. (local_setattro): Update. * mi/mi-cmd-var.c (print_varobj, mi_cmd_var_list_children) (varobj_update_one): Update. --- gdb/python/py-cmd.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'gdb/python/py-cmd.c') diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index af6c5cf..8333789 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -156,7 +156,6 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty) if (! result) { PyObject *ptype, *pvalue, *ptraceback; - char *msg; PyErr_Fetch (&ptype, &pvalue, &ptraceback); @@ -164,8 +163,8 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty) When fetching the error message we need to make our own copy, we no longer own ptype, pvalue after the call to PyErr_Restore. */ - msg = gdbpy_exception_to_string (ptype, pvalue); - make_cleanup (xfree, msg); + gdb::unique_xmalloc_ptr + msg (gdbpy_exception_to_string (ptype, pvalue)); if (msg == NULL) { @@ -190,7 +189,7 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty) PyErr_Restore (ptype, pvalue, ptraceback); gdbpy_print_stack (); if (msg != NULL && *msg != '\0') - error (_("Error occurred in Python command: %s"), msg); + error (_("Error occurred in Python command: %s"), msg.get ()); else error (_("Error occurred in Python command.")); } @@ -199,7 +198,7 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty) Py_XDECREF (ptype); Py_XDECREF (pvalue); Py_XDECREF (ptraceback); - error ("%s", msg); + error ("%s", msg.get ()); } } @@ -374,7 +373,6 @@ cmdpy_completer (struct cmd_list_element *command, while ((elt = PyIter_Next (iter)) != NULL) { - char *item; if (! gdbpy_is_string (elt)) { @@ -382,7 +380,8 @@ cmdpy_completer (struct cmd_list_element *command, Py_DECREF (elt); continue; } - item = python_string_to_host_string (elt); + gdb::unique_xmalloc_ptr + item (python_string_to_host_string (elt)); Py_DECREF (elt); if (item == NULL) { @@ -390,7 +389,7 @@ cmdpy_completer (struct cmd_list_element *command, PyErr_Clear (); continue; } - VEC_safe_push (char_ptr, result, item); + VEC_safe_push (char_ptr, result, item.release ()); } Py_DECREF (iter); @@ -604,7 +603,7 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw) if (ds_obj && gdbpy_is_string (ds_obj)) { - docstring = python_string_to_host_string (ds_obj); + docstring = python_string_to_host_string (ds_obj).release (); if (docstring == NULL) { xfree (cmd_name); -- cgit v1.1