START_LIST is the list in which the search starts.
- This function returns the xmalloc()d name of the new command. On
- error sets the Python error and returns NULL. */
+ This function returns the name of the new command. On error sets the Python
+ error and returns NULL. */
-char *
+gdb::unique_xmalloc_ptr<char>
gdbpy_parse_command_name (const char *name,
struct cmd_list_element ***base_list,
struct cmd_list_element **start_list)
int len = strlen (name);
int i, lastchar;
const char *prefix_text2;
- char *result;
/* Skip trailing whitespace. */
for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
/* Find first character of the final word. */
for (; i > 0 && valid_cmd_char_p (name[i - 1]); --i)
;
- result = (char *) xmalloc (lastchar - i + 2);
- memcpy (result, &name[i], lastchar - i + 1);
- result[lastchar - i + 1] = '\0';
+
+ gdb::unique_xmalloc_ptr<char> result ((char *) xmalloc (lastchar - i + 2));
+ memcpy (result.get (), &name[i], lastchar - i + 1);
+ result.get ()[lastchar - i + 1] = '\0';
/* Skip whitespace again. */
for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
{
PyErr_Format (PyExc_RuntimeError, _("Could not find command prefix %s."),
prefix_text.c_str ());
- xfree (result);
return NULL;
}
PyErr_Format (PyExc_RuntimeError, _("'%s' is not a prefix command."),
prefix_text.c_str ());
- xfree (result);
return NULL;
}
int completetype = -1;
char *docstring = NULL;
struct cmd_list_element **cmd_list;
- char *cmd_name;
static const char *keywords[] = { "name", "command_class", "completer_class",
"prefix", NULL };
PyObject *is_prefix_obj = NULL;
return -1;
}
- cmd_name = gdbpy_parse_command_name (name, &cmd_list, &cmdlist);
- if (! cmd_name)
+ gdb::unique_xmalloc_ptr<char> cmd_name
+ = gdbpy_parse_command_name (name, &cmd_list, &cmdlist);
+ if (cmd_name == nullptr)
return -1;
if (is_prefix_obj != NULL)
{
int cmp = PyObject_IsTrue (is_prefix_obj);
- if (cmp < 0)
- {
- xfree (cmd_name);
- return -1;
- }
- is_prefix = cmp > 0;
+ if (cmp < 0)
+ return -1;
+
+ is_prefix = cmp > 0;
}
if (PyObject_HasAttr (self, gdbpy_doc_cst))
{
docstring = python_string_to_host_string (ds_obj.get ()).release ();
if (docstring == NULL)
- {
- xfree (cmd_name);
- return -1;
- }
+ return -1;
}
}
if (! docstring)
/* If we have our own "invoke" method, then allow unknown
sub-commands. */
allow_unknown = PyObject_HasAttr (self, invoke_cst);
- cmd = add_prefix_cmd (cmd_name, (enum command_class) cmdtype,
+ cmd = add_prefix_cmd (cmd_name.get (),
+ (enum command_class) cmdtype,
NULL, docstring, &obj->sub_list,
allow_unknown, cmd_list);
}
else
- cmd = add_cmd (cmd_name, (enum command_class) cmdtype,
+ cmd = add_cmd (cmd_name.get (), (enum command_class) cmdtype,
docstring, cmd_list);
+ /* If successful, the above takes ownership of the name, since we set
+ name_allocated, so release it. */
+ cmd_name.release ();
+
/* There appears to be no API to set this. */
cmd->func = cmdpy_function;
cmd->destroyer = cmdpy_destroyer;
}
catch (const gdb_exception &except)
{
- xfree (cmd_name);
xfree (docstring);
gdbpy_convert_exception (except);
return -1;
function. */
static void
add_setshow_generic (int parmclass, enum command_class cmdclass,
- const char *cmd_name, parmpy_object *self,
+ gdb::unique_xmalloc_ptr<char> cmd_name,
+ parmpy_object *self,
const char *set_doc, const char *show_doc,
const char *help_doc,
struct cmd_list_element **set_list,
{
case var_boolean:
- add_setshow_boolean_cmd (cmd_name, cmdclass,
+ add_setshow_boolean_cmd (cmd_name.get (), cmdclass,
&self->value.boolval, set_doc, show_doc,
help_doc, get_set_value, get_show_value,
set_list, show_list);
break;
case var_auto_boolean:
- add_setshow_auto_boolean_cmd (cmd_name, cmdclass,
+ add_setshow_auto_boolean_cmd (cmd_name.get (), cmdclass,
&self->value.autoboolval,
set_doc, show_doc, help_doc,
get_set_value, get_show_value,
break;
case var_uinteger:
- add_setshow_uinteger_cmd (cmd_name, cmdclass,
+ add_setshow_uinteger_cmd (cmd_name.get (), cmdclass,
&self->value.uintval, set_doc, show_doc,
help_doc, get_set_value, get_show_value,
set_list, show_list);
break;
case var_integer:
- add_setshow_integer_cmd (cmd_name, cmdclass,
+ add_setshow_integer_cmd (cmd_name.get (), cmdclass,
&self->value.intval, set_doc, show_doc,
help_doc, get_set_value, get_show_value,
set_list, show_list); break;
case var_string:
- add_setshow_string_cmd (cmd_name, cmdclass,
+ add_setshow_string_cmd (cmd_name.get (), cmdclass,
&self->value.stringval, set_doc, show_doc,
help_doc, get_set_value, get_show_value,
set_list, show_list); break;
case var_string_noescape:
- add_setshow_string_noescape_cmd (cmd_name, cmdclass,
+ add_setshow_string_noescape_cmd (cmd_name.get (), cmdclass,
&self->value.stringval,
set_doc, show_doc, help_doc,
get_set_value, get_show_value,
break;
case var_optional_filename:
- add_setshow_optional_filename_cmd (cmd_name, cmdclass,
+ add_setshow_optional_filename_cmd (cmd_name.get (), cmdclass,
&self->value.stringval, set_doc,
show_doc, help_doc, get_set_value,
get_show_value, set_list,
break;
case var_filename:
- add_setshow_filename_cmd (cmd_name, cmdclass,
+ add_setshow_filename_cmd (cmd_name.get (), cmdclass,
&self->value.stringval, set_doc, show_doc,
help_doc, get_set_value, get_show_value,
set_list, show_list); break;
case var_zinteger:
- add_setshow_zinteger_cmd (cmd_name, cmdclass,
+ add_setshow_zinteger_cmd (cmd_name.get (), cmdclass,
&self->value.intval, set_doc, show_doc,
help_doc, get_set_value, get_show_value,
set_list, show_list);
break;
case var_zuinteger:
- add_setshow_zuinteger_cmd (cmd_name, cmdclass,
+ add_setshow_zuinteger_cmd (cmd_name.get (), cmdclass,
&self->value.uintval, set_doc, show_doc,
help_doc, get_set_value, get_show_value,
set_list, show_list);
break;
case var_zuinteger_unlimited:
- add_setshow_zuinteger_unlimited_cmd (cmd_name, cmdclass,
+ add_setshow_zuinteger_unlimited_cmd (cmd_name.get (), cmdclass,
&self->value.intval, set_doc,
show_doc, help_doc, get_set_value,
get_show_value,
break;
case var_enum:
- add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
+ add_setshow_enum_cmd (cmd_name.get (), cmdclass, self->enumeration,
&self->value.cstringval, set_doc, show_doc,
help_doc, get_set_value, get_show_value,
set_list, show_list);
/* Lookup created parameter, and register Python object against the
parameter context. Perform this task against both lists. */
- tmp_name = cmd_name;
+ tmp_name = cmd_name.get ();
param = lookup_cmd (&tmp_name, *show_list, "", NULL, 0, 1);
if (param)
set_cmd_context (param, self);
- tmp_name = cmd_name;
+ tmp_name = cmd_name.get ();
param = lookup_cmd (&tmp_name, *set_list, "", NULL, 0, 1);
if (param)
set_cmd_context (param, self);
+
+ /* We (unfortunately) currently leak the command name. */
+ cmd_name.release ();
}
/* A helper which computes enum values. Returns 1 on success. Returns 0 on
parmpy_object *obj = (parmpy_object *) self;
const char *name;
gdb::unique_xmalloc_ptr<char> set_doc, show_doc, doc;
- char *cmd_name;
int parmclass, cmdtype;
PyObject *enum_values = NULL;
struct cmd_list_element **set_list, **show_list;
obj->type = (enum var_types) parmclass;
memset (&obj->value, 0, sizeof (obj->value));
- cmd_name = gdbpy_parse_command_name (name, &set_list,
- &setlist);
-
- if (! cmd_name)
+ gdb::unique_xmalloc_ptr<char> cmd_name
+ = gdbpy_parse_command_name (name, &set_list, &setlist);
+ if (cmd_name == nullptr)
return -1;
- xfree (cmd_name);
- cmd_name = gdbpy_parse_command_name (name, &show_list,
- &showlist);
- if (! cmd_name)
+
+ cmd_name = gdbpy_parse_command_name (name, &show_list, &showlist);
+ if (cmd_name == nullptr)
return -1;
set_doc = get_doc_string (self, set_doc_cst);
try
{
add_setshow_generic (parmclass, (enum command_class) cmdtype,
- cmd_name, obj,
+ std::move (cmd_name), obj,
set_doc.get (), show_doc.get (),
doc.get (), set_list, show_list);
}
catch (const gdb_exception &except)
{
- xfree (cmd_name);
Py_DECREF (self);
gdbpy_convert_exception (except);
return -1;