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.
This commit is contained in:
Tom Tromey
2016-10-15 09:20:02 -06:00
parent 4e9d215322
commit 9b9720149d
19 changed files with 207 additions and 225 deletions

View File

@ -667,7 +667,7 @@ valpy_getitem (PyObject *self, PyObject *key)
{
struct gdb_exception except = exception_none;
value_object *self_value = (value_object *) self;
char *field = NULL;
gdb::unique_xmalloc_ptr<char> field;
struct type *base_class_type = NULL, *field_type = NULL;
long bitpos = -1;
PyObject *result = NULL;
@ -754,7 +754,7 @@ valpy_getitem (PyObject *self, PyObject *key)
struct value *res_val = NULL;
if (field)
res_val = value_struct_elt (&tmp, NULL, field, NULL,
res_val = value_struct_elt (&tmp, NULL, field.get (), NULL,
"struct/class/union");
else if (bitpos >= 0)
res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type,
@ -804,7 +804,6 @@ valpy_getitem (PyObject *self, PyObject *key)
}
END_CATCH
xfree (field);
GDB_PY_HANDLE_EXCEPTION (except);
return result;
@ -1663,17 +1662,11 @@ convert_value_from_python (PyObject *obj)
}
else if (gdbpy_is_string (obj))
{
char *s;
s = python_string_to_target_string (obj);
gdb::unique_xmalloc_ptr<char> s
= python_string_to_target_string (obj);
if (s != NULL)
{
struct cleanup *old;
old = make_cleanup (xfree, s);
value = value_cstring (s, strlen (s), builtin_type_pychar);
do_cleanups (old);
}
value = value_cstring (s.get (), strlen (s.get ()),
builtin_type_pychar);
}
else if (PyObject_TypeCheck (obj, &value_object_type))
value = value_copy (((value_object *) obj)->value);