From 075c55e0cc0a68eeab777027213c2f545618e844 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 26 Dec 2018 11:05:57 -0700 Subject: Remove more calls to xfree from Python This changes the Python code to remove some more calls to xfree, in favor of self-managing data structures. Tested on x86-64 Fedora 28. gdb/ChangeLog 2018-12-27 Tom Tromey * python/python.c (python_interactive_command): Use std::string. (gdbpy_parameter): Likewise. * python/py-utils.c (unicode_to_encoded_string): Update comment. * python/py-symtab.c (salpy_str): Use PyString_FromFormat. * python/py-record-btrace.c (recpy_bt_insn_data): Use byte_vector. * python/py-objfile.c (objfpy_get_build_id): Use unique_xmalloc_ptr. * python/py-inferior.c (infpy_read_memory): Use unique_xmalloc_ptr. * python/py-cmd.c (gdbpy_parse_command_name): Use std::string. --- gdb/ChangeLog | 14 ++++++++++++++ gdb/python/py-cmd.c | 14 ++++---------- gdb/python/py-inferior.c | 14 +++++--------- gdb/python/py-objfile.c | 8 +++----- gdb/python/py-record-btrace.c | 11 +++++------ gdb/python/py-symtab.c | 11 ++--------- gdb/python/py-utils.c | 5 ++--- gdb/python/python.c | 20 +++++--------------- 8 files changed, 40 insertions(+), 57 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 397ee04..a6adc53 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,17 @@ +2018-12-27 Tom Tromey + + * python/python.c (python_interactive_command): Use std::string. + (gdbpy_parameter): Likewise. + * python/py-utils.c (unicode_to_encoded_string): Update comment. + * python/py-symtab.c (salpy_str): Use PyString_FromFormat. + * python/py-record-btrace.c (recpy_bt_insn_data): Use + byte_vector. + * python/py-objfile.c (objfpy_get_build_id): Use + unique_xmalloc_ptr. + * python/py-inferior.c (infpy_read_memory): Use + unique_xmalloc_ptr. + * python/py-cmd.c (gdbpy_parse_command_name): Use std::string. + 2018-12-26 Simon Marchi * target.c (target_terminal::restore_inferior): Remove struct keyword. diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index d560b3a..0e39730 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -362,7 +362,6 @@ gdbpy_parse_command_name (const char *name, struct cmd_list_element *elt; int len = strlen (name); int i, lastchar; - char *prefix_text; const char *prefix_text2; char *result; @@ -395,31 +394,26 @@ gdbpy_parse_command_name (const char *name, return result; } - prefix_text = (char *) xmalloc (i + 2); - memcpy (prefix_text, name, i + 1); - prefix_text[i + 1] = '\0'; + std::string prefix_text (name, i + 1); - prefix_text2 = prefix_text; + prefix_text2 = prefix_text.c_str (); elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, 1); if (elt == NULL || elt == CMD_LIST_AMBIGUOUS) { PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."), - prefix_text); - xfree (prefix_text); + prefix_text.c_str ()); xfree (result); return NULL; } if (elt->prefixlist) { - xfree (prefix_text); *base_list = elt->prefixlist; return result; } PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."), - prefix_text); - xfree (prefix_text); + prefix_text.c_str ()); xfree (result); return NULL; } diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 7b378ca..f68f6ea 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -499,7 +499,7 @@ static PyObject * infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) { CORE_ADDR addr, length; - gdb_byte *buffer = NULL; + gdb::unique_xmalloc_ptr buffer; PyObject *addr_obj, *length_obj, *result; static const char *keywords[] = { "address", "length", NULL }; @@ -513,13 +513,12 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) TRY { - buffer = (gdb_byte *) xmalloc (length); + buffer.reset ((gdb_byte *) xmalloc (length)); - read_memory (addr, buffer, length); + read_memory (addr, buffer.get (), length); } CATCH (except, RETURN_MASK_ALL) { - xfree (buffer); GDB_PY_HANDLE_EXCEPTION (except); } END_CATCH @@ -527,12 +526,9 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) gdbpy_ref membuf_obj (PyObject_New (membuf_object, &membuf_object_type)); if (membuf_obj == NULL) - { - xfree (buffer); - return NULL; - } + return NULL; - membuf_obj->buffer = buffer; + membuf_obj->buffer = buffer.release (); membuf_obj->addr = addr; membuf_obj->length = length; diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index dc7f342..d15d5ec 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -143,12 +143,10 @@ objfpy_get_build_id (PyObject *self, void *closure) if (build_id != NULL) { - char *hex_form = make_hex_string (build_id->data, build_id->size); - PyObject *result; + gdb::unique_xmalloc_ptr hex_form + (make_hex_string (build_id->data, build_id->size)); - result = host_string_to_python_string (hex_form).release (); - xfree (hex_form); - return result; + return host_string_to_python_string (hex_form.get ()).release (); } Py_RETURN_NONE; diff --git a/gdb/python/py-record-btrace.c b/gdb/python/py-record-btrace.c index 44cb441..609e61b 100644 --- a/gdb/python/py-record-btrace.c +++ b/gdb/python/py-record-btrace.c @@ -273,7 +273,7 @@ PyObject * recpy_bt_insn_data (PyObject *self, void *closure) { const btrace_insn * const insn = btrace_insn_from_recpy_insn (self); - gdb_byte *buffer = NULL; + gdb::byte_vector buffer; PyObject *object; if (insn == NULL) @@ -281,18 +281,17 @@ recpy_bt_insn_data (PyObject *self, void *closure) TRY { - buffer = (gdb_byte *) xmalloc (insn->size); - read_memory (insn->pc, buffer, insn->size); + buffer.resize (insn->size); + read_memory (insn->pc, buffer.data (), insn->size); } CATCH (except, RETURN_MASK_ALL) { - xfree (buffer); GDB_PY_HANDLE_EXCEPTION (except); } END_CATCH - object = PyBytes_FromStringAndSize ((const char*) buffer, insn->size); - xfree (buffer); + object = PyBytes_FromStringAndSize ((const char *) buffer.data (), + insn->size); if (object == NULL) return NULL; diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c index 4b29299..d1326e3 100644 --- a/gdb/python/py-symtab.c +++ b/gdb/python/py-symtab.c @@ -220,10 +220,8 @@ stpy_get_linetable (PyObject *self, PyObject *args) static PyObject * salpy_str (PyObject *self) { - char *s; const char *filename; sal_object *sal_obj; - PyObject *result; struct symtab_and_line *sal = NULL; SALPY_REQUIRE_VALID (self, sal); @@ -232,13 +230,8 @@ salpy_str (PyObject *self) filename = (sal_obj->symtab == (symtab_object *) Py_None) ? "" : symtab_to_filename_for_display (sal_obj->symtab->symtab); - s = xstrprintf ("symbol and line for %s, line %d", filename, - sal->line); - - result = PyString_FromString (s); - xfree (s); - - return result; + return PyString_FromFormat ("symbol and line for %s, line %d", filename, + sal->line); } static void diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 2b47790..e0aedb5 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -62,9 +62,8 @@ python_string_to_unicode (PyObject *obj) /* Returns a newly allocated string with the contents of the given unicode string object converted to 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. */ + conversion, NULL will be returned and a python exception will be + set. */ static gdb::unique_xmalloc_ptr unicode_to_encoded_string (PyObject *unicode_str, const char *charset) { diff --git a/gdb/python/python.c b/gdb/python/python.c index d6453e7..f790d48 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -296,14 +296,8 @@ python_interactive_command (const char *arg, int from_tty) if (arg && *arg) { - int len = strlen (arg); - char *script = (char *) xmalloc (len + 2); - - strcpy (script, arg); - script[len] = '\n'; - script[len + 1] = '\0'; - err = eval_python_command (script); - xfree (script); + std::string script = std::string (arg) + "\n"; + err = eval_python_command (script.c_str ()); } else { @@ -495,29 +489,25 @@ gdbpy_parameter_value (enum var_types type, void *var) static PyObject * gdbpy_parameter (PyObject *self, PyObject *args) { - struct gdb_exception except = exception_none; struct cmd_list_element *alias, *prefix, *cmd; const char *arg; - char *newarg; int found = -1; if (! PyArg_ParseTuple (args, "s", &arg)) return NULL; - newarg = concat ("show ", arg, (char *) NULL); + std::string newarg = std::string ("show ") + arg; TRY { - found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd); + found = lookup_cmd_composition (newarg.c_str (), &alias, &prefix, &cmd); } CATCH (ex, RETURN_MASK_ALL) { - except = ex; + GDB_PY_HANDLE_EXCEPTION (ex); } END_CATCH - xfree (newarg); - GDB_PY_HANDLE_EXCEPTION (except); if (!found) return PyErr_Format (PyExc_RuntimeError, _("Could not find parameter `%s'."), arg); -- cgit v1.1