mirror of
https://github.com/espressif/binutils-gdb.git
synced 2025-09-18 15:43:08 +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:
@ -107,10 +107,10 @@ python_string_to_unicode (PyObject *obj)
|
||||
conversion, NULL will be returned and a python exception will be set.
|
||||
|
||||
The caller is responsible for xfree'ing the string. */
|
||||
static char *
|
||||
static gdb::unique_xmalloc_ptr<char>
|
||||
unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
|
||||
{
|
||||
char *result;
|
||||
gdb::unique_xmalloc_ptr<char> result;
|
||||
PyObject *string;
|
||||
|
||||
/* Translate string to named charset. */
|
||||
@ -119,9 +119,9 @@ unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
|
||||
return NULL;
|
||||
|
||||
#ifdef IS_PY3K
|
||||
result = xstrdup (PyBytes_AsString (string));
|
||||
result.reset (xstrdup (PyBytes_AsString (string)));
|
||||
#else
|
||||
result = xstrdup (PyString_AsString (string));
|
||||
result.reset (xstrdup (PyString_AsString (string)));
|
||||
#endif
|
||||
|
||||
Py_DECREF (string);
|
||||
@ -140,12 +140,11 @@ unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
|
||||
return PyUnicode_AsEncodedString (unicode_str, charset, NULL);
|
||||
}
|
||||
|
||||
/* Returns a newly allocated string with the contents of the given unicode
|
||||
string object converted to the target's charset. If an error occurs during
|
||||
the conversion, NULL will be returned and a python exception will be set.
|
||||
|
||||
The caller is responsible for xfree'ing the string. */
|
||||
char *
|
||||
/* Returns a newly allocated string with the contents of the given
|
||||
unicode string object converted to the target's charset. If an
|
||||
error occurs during the conversion, NULL will be returned and a
|
||||
python exception will be set. */
|
||||
gdb::unique_xmalloc_ptr<char>
|
||||
unicode_to_target_string (PyObject *unicode_str)
|
||||
{
|
||||
return unicode_to_encoded_string (unicode_str,
|
||||
@ -164,20 +163,18 @@ unicode_to_target_python_string (PyObject *unicode_str)
|
||||
}
|
||||
|
||||
/* Converts a python string (8-bit or unicode) to a target string in
|
||||
the target's charset. Returns NULL on error, with a python exception set.
|
||||
|
||||
The caller is responsible for xfree'ing the string. */
|
||||
char *
|
||||
the target's charset. Returns NULL on error, with a python
|
||||
exception set. */
|
||||
gdb::unique_xmalloc_ptr<char>
|
||||
python_string_to_target_string (PyObject *obj)
|
||||
{
|
||||
PyObject *str;
|
||||
char *result;
|
||||
|
||||
str = python_string_to_unicode (obj);
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
|
||||
result = unicode_to_target_string (str);
|
||||
gdb::unique_xmalloc_ptr<char> result (unicode_to_target_string (str));
|
||||
Py_DECREF (str);
|
||||
return result;
|
||||
}
|
||||
@ -203,20 +200,19 @@ python_string_to_target_python_string (PyObject *obj)
|
||||
}
|
||||
|
||||
/* Converts a python string (8-bit or unicode) to a target string in
|
||||
the host's charset. Returns NULL on error, with a python exception set.
|
||||
|
||||
The caller is responsible for xfree'ing the string. */
|
||||
char *
|
||||
the host's charset. Returns NULL on error, with a python exception
|
||||
set. */
|
||||
gdb::unique_xmalloc_ptr<char>
|
||||
python_string_to_host_string (PyObject *obj)
|
||||
{
|
||||
PyObject *str;
|
||||
char *result;
|
||||
|
||||
str = python_string_to_unicode (obj);
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
|
||||
result = unicode_to_encoded_string (str, host_charset ());
|
||||
gdb::unique_xmalloc_ptr<char>
|
||||
result (unicode_to_encoded_string (str, host_charset ()));
|
||||
Py_DECREF (str);
|
||||
return result;
|
||||
}
|
||||
@ -243,20 +239,21 @@ gdbpy_is_string (PyObject *obj)
|
||||
}
|
||||
|
||||
/* Return the string representation of OBJ, i.e., str (obj).
|
||||
Space for the result is malloc'd, the caller must free.
|
||||
If the result is NULL a python error occurred, the caller must clear it. */
|
||||
|
||||
char *
|
||||
gdb::unique_xmalloc_ptr<char>
|
||||
gdbpy_obj_to_string (PyObject *obj)
|
||||
{
|
||||
PyObject *str_obj = PyObject_Str (obj);
|
||||
|
||||
if (str_obj != NULL)
|
||||
{
|
||||
gdb::unique_xmalloc_ptr<char> msg;
|
||||
|
||||
#ifdef IS_PY3K
|
||||
char *msg = python_string_to_host_string (str_obj);
|
||||
msg = python_string_to_host_string (str_obj);
|
||||
#else
|
||||
char *msg = xstrdup (PyString_AsString (str_obj));
|
||||
msg.reset (xstrdup (PyString_AsString (str_obj)));
|
||||
#endif
|
||||
|
||||
Py_DECREF (str_obj);
|
||||
@ -269,14 +266,11 @@ gdbpy_obj_to_string (PyObject *obj)
|
||||
/* Return the string representation of the exception represented by
|
||||
TYPE, VALUE which is assumed to have been obtained with PyErr_Fetch,
|
||||
i.e., the error indicator is currently clear.
|
||||
Space for the result is malloc'd, the caller must free.
|
||||
If the result is NULL a python error occurred, the caller must clear it. */
|
||||
|
||||
char *
|
||||
gdb::unique_xmalloc_ptr<char>
|
||||
gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue)
|
||||
{
|
||||
char *str;
|
||||
|
||||
/* There are a few cases to consider.
|
||||
For example:
|
||||
pvalue is a string when PyErr_SetString is used.
|
||||
@ -288,11 +282,9 @@ gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue)
|
||||
gdb.GdbError ("message"). */
|
||||
|
||||
if (pvalue && pvalue != Py_None)
|
||||
str = gdbpy_obj_to_string (pvalue);
|
||||
return gdbpy_obj_to_string (pvalue);
|
||||
else
|
||||
str = gdbpy_obj_to_string (ptype);
|
||||
|
||||
return str;
|
||||
return gdbpy_obj_to_string (ptype);
|
||||
}
|
||||
|
||||
/* Convert a GDB exception to the appropriate Python exception.
|
||||
|
Reference in New Issue
Block a user