summaryrefslogtreecommitdiff
path: root/gdb/python/py-param.c
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2016-10-15 09:20:02 -0600
committerTom Tromey <tom@tromey.com>2016-11-09 19:40:12 -0700
commit9b9720149dfee4a9a961c29d0382fc5bdf9c975b (patch)
tree2cc05b39a9e63376818a11e7f56954f25f9fb963 /gdb/python/py-param.c
parent4e9d2153228d95c972907a8b13237218d380c5d3 (diff)
downloadbinutils-gdb-9b9720149dfee4a9a961c29d0382fc5bdf9c975b.tar.gz
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 <tom@tromey.com> * 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.
Diffstat (limited to 'gdb/python/py-param.c')
-rw-r--r--gdb/python/py-param.c46
1 files changed, 20 insertions, 26 deletions
diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c
index 3604f9f52aa..0d19c97efb8 100644
--- a/gdb/python/py-param.c
+++ b/gdb/python/py-param.c
@@ -148,21 +148,19 @@ set_parameter_value (parmpy_object *self, PyObject *value)
}
else
{
- char *string;
-
- string = python_string_to_host_string (value);
+ gdb::unique_xmalloc_ptr<char>
+ string (python_string_to_host_string (value));
if (string == NULL)
return -1;
xfree (self->value.stringval);
- self->value.stringval = string;
+ self->value.stringval = string.release ();
}
break;
case var_enum:
{
int i;
- char *str;
if (! gdbpy_is_string (value))
{
@@ -171,13 +169,13 @@ set_parameter_value (parmpy_object *self, PyObject *value)
return -1;
}
- str = python_string_to_host_string (value);
+ gdb::unique_xmalloc_ptr<char>
+ str (python_string_to_host_string (value));
if (str == NULL)
return -1;
for (i = 0; self->enumeration[i]; ++i)
- if (! strcmp (self->enumeration[i], str))
+ if (! strcmp (self->enumeration[i], str.get ()))
break;
- xfree (str);
if (! self->enumeration[i])
{
PyErr_SetString (PyExc_RuntimeError,
@@ -301,10 +299,10 @@ set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
/* A helper function which returns a documentation string for an
object. */
-static char *
+static gdb::unique_xmalloc_ptr<char>
get_doc_string (PyObject *object, PyObject *attr)
{
- char *result = NULL;
+ gdb::unique_xmalloc_ptr<char> result;
if (PyObject_HasAttr (object, attr))
{
@@ -319,7 +317,7 @@ get_doc_string (PyObject *object, PyObject *attr)
Py_XDECREF (ds_obj);
}
if (! result)
- result = xstrdup (_("This command is not documented."));
+ result.reset (xstrdup (_("This command is not documented.")));
return result;
}
@@ -327,10 +325,10 @@ get_doc_string (PyObject *object, PyObject *attr)
argument ARG. ARG can be NULL. METHOD should return a Python
string. If this function returns NULL, there has been an error and
the appropriate exception set. */
-static char *
+static gdb::unique_xmalloc_ptr<char>
call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
{
- char *data = NULL;
+ gdb::unique_xmalloc_ptr<char> data;
PyObject *result = PyObject_CallMethodObjArgs (obj, method, arg, NULL);
if (! result)
@@ -365,7 +363,7 @@ get_set_value (char *args, int from_tty,
struct cmd_list_element *c)
{
PyObject *obj = (PyObject *) get_cmd_context (c);
- char *set_doc_string;
+ gdb::unique_xmalloc_ptr<char> set_doc_string;
struct cleanup *cleanup = ensure_python_env (get_current_arch (),
current_language);
PyObject *set_doc_func = PyString_FromString ("get_set_string");
@@ -387,8 +385,7 @@ get_set_value (char *args, int from_tty,
set_doc_string = get_doc_string (obj, set_doc_cst);
}
- make_cleanup (xfree, set_doc_string);
- fprintf_filtered (gdb_stdout, "%s\n", set_doc_string);
+ fprintf_filtered (gdb_stdout, "%s\n", set_doc_string.get ());
Py_XDECREF (set_doc_func);
do_cleanups (cleanup);
@@ -413,7 +410,7 @@ get_show_value (struct ui_file *file, int from_tty,
const char *value)
{
PyObject *obj = (PyObject *) get_cmd_context (c);
- char *show_doc_string = NULL;
+ gdb::unique_xmalloc_ptr<char> show_doc_string;
struct cleanup *cleanup = ensure_python_env (get_current_arch (),
current_language);
PyObject *show_doc_func = PyString_FromString ("get_show_string");
@@ -433,9 +430,7 @@ get_show_value (struct ui_file *file, int from_tty,
if (! show_doc_string)
goto error;
- make_cleanup (xfree, show_doc_string);
-
- fprintf_filtered (file, "%s\n", show_doc_string);
+ fprintf_filtered (file, "%s\n", show_doc_string.get ());
}
else
{
@@ -443,8 +438,7 @@ get_show_value (struct ui_file *file, int from_tty,
callback function does not exist, then attempt to read the
show_doc attribute. */
show_doc_string = get_doc_string (obj, show_doc_cst);
- make_cleanup (xfree, show_doc_string);
- fprintf_filtered (file, "%s %s\n", show_doc_string, value);
+ fprintf_filtered (file, "%s %s\n", show_doc_string.get (), value);
}
Py_XDECREF (show_doc_func);
@@ -614,7 +608,7 @@ compute_enum_values (parmpy_object *self, PyObject *enum_values)
_("The enumeration item not a string."));
return 0;
}
- self->enumeration[i] = python_string_to_host_string (item);
+ self->enumeration[i] = python_string_to_host_string (item).release ();
Py_DECREF (item);
if (self->enumeration[i] == NULL)
{
@@ -716,9 +710,9 @@ parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
if (! cmd_name)
return -1;
- set_doc = get_doc_string (self, set_doc_cst);
- show_doc = get_doc_string (self, show_doc_cst);
- doc = get_doc_string (self, gdbpy_doc_cst);
+ set_doc = get_doc_string (self, set_doc_cst).release ();
+ show_doc = get_doc_string (self, show_doc_cst).release ();
+ doc = get_doc_string (self, gdbpy_doc_cst).release ();
Py_INCREF (self);