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

@ -244,11 +244,11 @@ pretty_print_one_value (PyObject *printer, struct value **out_value)
NULL if there is no display_hint method, or if the method did not
return a string. On error, print stack trace and return NULL. On
success, return an xmalloc()d string. */
char *
gdb::unique_xmalloc_ptr<char>
gdbpy_get_display_hint (PyObject *printer)
{
PyObject *hint;
char *result = NULL;
gdb::unique_xmalloc_ptr<char> result;
if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
return NULL;
@ -279,20 +279,20 @@ print_stack_unless_memory_error (struct ui_file *stream)
{
struct cleanup *cleanup;
PyObject *type, *value, *trace;
char *msg;
PyErr_Fetch (&type, &value, &trace);
cleanup = make_cleanup_py_decref (type);
make_cleanup_py_decref (value);
make_cleanup_py_decref (trace);
msg = gdbpy_exception_to_string (type, value);
make_cleanup (xfree, msg);
gdb::unique_xmalloc_ptr<char>
msg (gdbpy_exception_to_string (type, value));
if (msg == NULL || *msg == '\0')
fprintf_filtered (stream, _("<error reading variable>"));
else
fprintf_filtered (stream, _("<error reading variable: %s>"), msg);
fprintf_filtered (stream, _("<error reading variable: %s>"),
msg.get ());
do_cleanups (cleanup);
}
@ -647,16 +647,13 @@ print_children (PyObject *printer, const char *hint,
}
else if (gdbpy_is_string (py_v))
{
char *output;
gdb::unique_xmalloc_ptr<char> output;
output = python_string_to_host_string (py_v);
if (!output)
gdbpy_print_stack ();
else
{
fputs_filtered (output, stream);
xfree (output);
}
fputs_filtered (output.get (), stream);
}
else
{
@ -713,7 +710,7 @@ gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
PyObject *printer = NULL;
PyObject *val_obj = NULL;
struct value *value;
char *hint = NULL;
gdb::unique_xmalloc_ptr<char> hint;
struct cleanup *cleanups;
enum ext_lang_rc result = EXT_LANG_RC_NOP;
enum string_repr_result print_result;
@ -767,13 +764,12 @@ gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
/* If we are printing a map, we want some special formatting. */
hint = gdbpy_get_display_hint (printer);
make_cleanup (free_current_contents, &hint);
/* Print the section */
print_result = print_string_repr (printer, hint, stream, recurse,
print_result = print_string_repr (printer, hint.get (), stream, recurse,
options, language, gdbarch);
if (print_result != string_repr_error)
print_children (printer, hint, stream, recurse, options, language,
print_children (printer, hint.get (), stream, recurse, options, language,
print_result == string_repr_none);
result = EXT_LANG_RC_OK;