mirror of
https://github.com/espressif/binutils-gdb.git
synced 2025-05-23 18:26:47 +08:00
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:
@ -1020,7 +1020,7 @@ gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
|
||||
const char *current_gdb_prompt)
|
||||
{
|
||||
struct cleanup *cleanup;
|
||||
char *prompt = NULL;
|
||||
gdb::unique_xmalloc_ptr<char> prompt;
|
||||
|
||||
if (!gdb_python_initialized)
|
||||
return EXT_LANG_RC_NOP;
|
||||
@ -1073,8 +1073,6 @@ gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
|
||||
|
||||
if (prompt == NULL)
|
||||
goto fail;
|
||||
else
|
||||
make_cleanup (xfree, prompt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1082,7 +1080,7 @@ gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
|
||||
/* If a prompt has been set, PROMPT will not be NULL. If it is
|
||||
NULL, do not set the prompt. */
|
||||
if (prompt != NULL)
|
||||
set_prompt (prompt);
|
||||
set_prompt (prompt.get ());
|
||||
|
||||
do_cleanups (cleanup);
|
||||
return prompt != NULL ? EXT_LANG_RC_OK : EXT_LANG_RC_NOP;
|
||||
@ -1213,13 +1211,13 @@ gdbpy_print_stack (void)
|
||||
else
|
||||
{
|
||||
PyObject *ptype, *pvalue, *ptraceback;
|
||||
char *msg = NULL, *type = NULL;
|
||||
|
||||
PyErr_Fetch (&ptype, &pvalue, &ptraceback);
|
||||
|
||||
/* Fetch the error message contained within ptype, pvalue. */
|
||||
msg = gdbpy_exception_to_string (ptype, pvalue);
|
||||
type = gdbpy_obj_to_string (ptype);
|
||||
gdb::unique_xmalloc_ptr<char>
|
||||
msg (gdbpy_exception_to_string (ptype, pvalue));
|
||||
gdb::unique_xmalloc_ptr<char> type (gdbpy_obj_to_string (ptype));
|
||||
|
||||
TRY
|
||||
{
|
||||
@ -1233,7 +1231,7 @@ gdbpy_print_stack (void)
|
||||
}
|
||||
else
|
||||
fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
|
||||
type, msg);
|
||||
type.get (), msg.get ());
|
||||
}
|
||||
CATCH (except, RETURN_MASK_ALL)
|
||||
{
|
||||
@ -1243,7 +1241,6 @@ gdbpy_print_stack (void)
|
||||
Py_XDECREF (ptype);
|
||||
Py_XDECREF (pvalue);
|
||||
Py_XDECREF (ptraceback);
|
||||
xfree (msg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1448,7 +1445,7 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
|
||||
PyObject *type_obj, *type_module = NULL, *func = NULL;
|
||||
PyObject *result_obj = NULL;
|
||||
PyObject *printers_obj = (PyObject *) ext_printers->py_type_printers;
|
||||
char *result = NULL;
|
||||
gdb::unique_xmalloc_ptr<char> result;
|
||||
|
||||
if (printers_obj == NULL)
|
||||
return EXT_LANG_RC_NOP;
|
||||
@ -1501,8 +1498,11 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
|
||||
Py_XDECREF (result_obj);
|
||||
do_cleanups (cleanups);
|
||||
if (result != NULL)
|
||||
*prettied_type = result;
|
||||
return result != NULL ? EXT_LANG_RC_OK : EXT_LANG_RC_ERROR;
|
||||
{
|
||||
*prettied_type = result.release ();
|
||||
return EXT_LANG_RC_OK;
|
||||
}
|
||||
return EXT_LANG_RC_ERROR;
|
||||
}
|
||||
|
||||
/* Free the result of start_type_printers.
|
||||
|
Reference in New Issue
Block a user