]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Remove more calls to xfree from Python
authorTom Tromey <tom@tromey.com>
Wed, 26 Dec 2018 18:05:57 +0000 (11:05 -0700)
committerTom Tromey <tom@tromey.com>
Thu, 27 Dec 2018 17:50:43 +0000 (10:50 -0700)
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  <tom@tromey.com>

* 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
gdb/python/py-cmd.c
gdb/python/py-inferior.c
gdb/python/py-objfile.c
gdb/python/py-record-btrace.c
gdb/python/py-symtab.c
gdb/python/py-utils.c
gdb/python/python.c

index 397ee0458af25098626ffe90fed26ec360f5fbd9..a6adc5396607bd30fc912890b583998448b35800 100644 (file)
@@ -1,3 +1,17 @@
+2018-12-27  Tom Tromey  <tom@tromey.com>
+
+       * 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  <simon.marchi@polymtl.ca>
 
        * target.c (target_terminal::restore_inferior): Remove struct keyword.
index d560b3a44e3ee1fc14b0c1d6dc940f27a3e8ddc1..0e39730a4c012ec4a303738d1b1253710d01162c 100644 (file)
@@ -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;
 }
index 7b378ca48c779b1db9c0bbf7572a125c4b498215..f68f6ea6c016068bb52e71eb45f1abe262082e72 100644 (file)
@@ -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<gdb_byte> 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_object> 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;
 
index dc7f342aef46e8bd4730b10a0ea0cca7713f7165..d15d5eccaebb0450f1e0a8bd06ffaad3b3a90d5c 100644 (file)
@@ -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<char> 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;
index 44cb4419ea8f2fb77edeea2743e50bc39a14e880..609e61b7c8b7813703604554fd664d05951f0582 100644 (file)
@@ -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;
index 4b29299bf94abbf44cca534cc7c0f9932f747371..d1326e3e67f182242fef4a8579b2bd43e645a676 100644 (file)
@@ -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)
     ? "<unknown>" : 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
index 2b4779097b21c07616a4d01bd5228531b1ce23c1..e0aedb5b58e4e84979dd6536153a37f8942be260 100644 (file)
@@ -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<char>
 unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
 {
index d6453e786c98a231cd07a7d912cda201544ff3bc..f790d48cc2214c721e5ad78db0df35e0f90b9234 100644 (file)
@@ -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);