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  <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.
This commit is contained in:
Tom Tromey 2018-12-26 11:05:57 -07:00
parent 293bf1a719
commit 075c55e0cc
8 changed files with 40 additions and 57 deletions

View 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.

View 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;
}

View 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;

View 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;

View 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;

View 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

View 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)
{

View 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);