gdb/gdbsupport: make xstrprintf and xstrvprintf return a unique_ptr

The motivation is to reduce the number of places where unmanaged
pointers are returned from allocation type routines.  All of the
callers are updated.

There should be no user visible changes after this commit.
This commit is contained in:
Andrew Burgess 2021-11-08 14:58:46 +00:00
parent 2bb7589ddf
commit 8579fd136a
34 changed files with 119 additions and 151 deletions

View file

@ -13287,8 +13287,7 @@ public:
{
type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
std::string name = type_to_string (type);
return gdb::unique_xmalloc_ptr<char>
(xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr)));
return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
}
/* See language.h. */

View file

@ -1489,8 +1489,8 @@ void
_initialize_auto_load ()
{
struct cmd_list_element *cmd;
char *scripts_directory_help, *gdb_name_help, *python_name_help;
char *guile_name_help;
gdb::unique_xmalloc_ptr<char> scripts_directory_help, gdb_name_help,
python_name_help, guile_name_help;
const char *suffix;
gdb::observers::new_objfile.attach (auto_load_new_objfile,
@ -1565,23 +1565,18 @@ having 'set auto-load ... off'.\n\
Directories listed here need to be present also \
in the 'set auto-load safe-path'\n\
option."),
gdb_name_help,
python_name_help ? python_name_help : "",
guile_name_help ? guile_name_help : "");
gdb_name_help.get (),
python_name_help.get () ? python_name_help.get () : "",
guile_name_help.get () ? guile_name_help.get () : "");
add_setshow_optional_filename_cmd ("scripts-directory", class_support,
&auto_load_dir, _("\
Set the list of directories from which to load auto-loaded scripts."), _("\
Show the list of directories from which to load auto-loaded scripts."),
scripts_directory_help,
scripts_directory_help.get (),
set_auto_load_dir, show_auto_load_dir,
auto_load_set_cmdlist_get (),
auto_load_show_cmdlist_get ());
xfree (scripts_directory_help);
xfree (python_name_help);
xfree (gdb_name_help);
xfree (guile_name_help);
auto_load_safe_path_vec_update ();
add_setshow_optional_filename_cmd ("safe-path", class_support,
&auto_load_safe_path, _("\

View file

@ -8728,7 +8728,7 @@ static void
update_dprintf_command_list (struct breakpoint *b)
{
const char *dprintf_args = b->extra_string.get ();
char *printf_line = NULL;
gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
if (!dprintf_args)
return;
@ -8779,7 +8779,7 @@ update_dprintf_command_list (struct breakpoint *b)
/* Manufacture a printf sequence. */
struct command_line *printf_cmd_line
= new struct command_line (simple_control, printf_line);
= new struct command_line (simple_control, printf_line.release ());
breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
command_lines_deleter ()));
}
@ -10798,9 +10798,8 @@ watch_command_1 (const char *arg, int accessflag, int from_tty,
w->exp_string_reparse
= current_language->watch_location_expression (t, addr);
w->exp_string.reset (xstrprintf ("-location %.*s",
(int) (exp_end - exp_start),
exp_start));
w->exp_string = xstrprintf ("-location %.*s",
(int) (exp_end - exp_start), exp_start);
}
else
w->exp_string.reset (savestring (exp_start, exp_end - exp_start));

View file

@ -960,35 +960,31 @@ intermediate_encoding (void)
{
iconv_t desc;
static const char *stored_result = NULL;
char *result;
gdb::unique_xmalloc_ptr<char> result;
if (stored_result)
return stored_result;
result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8),
ENDIAN_SUFFIX);
/* Check that the name is supported by iconv_open. */
desc = iconv_open (result, host_charset ());
desc = iconv_open (result.get (), host_charset ());
if (desc != (iconv_t) -1)
{
iconv_close (desc);
stored_result = result;
return result;
stored_result = result.release ();
return stored_result;
}
/* Not valid, free the allocated memory. */
xfree (result);
/* Second try, with UCS-2 type. */
result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t),
ENDIAN_SUFFIX);
/* Check that the name is supported by iconv_open. */
desc = iconv_open (result, host_charset ());
desc = iconv_open (result.get (), host_charset ());
if (desc != (iconv_t) -1)
{
iconv_close (desc);
stored_result = result;
return result;
stored_result = result.release ();
return stored_result;
}
/* Not valid, free the allocated memory. */
xfree (result);
/* No valid charset found, generate error here. */
error (_("Unable to find a valid charset for string conversions"));
}

View file

@ -943,7 +943,6 @@ edit_command (const char *arg, int from_tty)
struct symtab_and_line sal;
struct symbol *sym;
const char *editor;
char *p;
const char *fn;
/* Pull in the current default source line if necessary. */
@ -1032,9 +1031,9 @@ edit_command (const char *arg, int from_tty)
/* Quote the file name, in case it has whitespace or other special
characters. */
p = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
shell_escape (p, from_tty);
xfree (p);
gdb::unique_xmalloc_ptr<char> p
= xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
shell_escape (p.get (), from_tty);
}
/* The options for the "pipe" command. */
@ -2730,7 +2729,7 @@ Usage: source [-s] [-v] FILE\n\
-v: each command in FILE is echoed as it is executed.\n\
\n\
Note that the file \"%s\" is read automatically in this way\n\
when GDB is started."), GDBINIT);
when GDB is started."), GDBINIT).release ();
c = add_cmd ("source", class_support, source_command,
source_help_text, &cmdlist);
set_cmd_completer (c, filename_completer);

View file

@ -534,8 +534,8 @@ add_setshow_cmd_full_erased (const char *name,
{
struct cmd_list_element *set;
struct cmd_list_element *show;
char *full_set_doc;
char *full_show_doc;
gdb::unique_xmalloc_ptr<char> full_set_doc;
gdb::unique_xmalloc_ptr<char> full_show_doc;
if (help_doc != NULL)
{
@ -544,18 +544,18 @@ add_setshow_cmd_full_erased (const char *name,
}
else
{
full_set_doc = xstrdup (set_doc);
full_show_doc = xstrdup (show_doc);
full_set_doc = make_unique_xstrdup (set_doc);
full_show_doc = make_unique_xstrdup (show_doc);
}
set = add_set_or_show_cmd (name, set_cmd, theclass, var_type, args,
full_set_doc, set_list);
full_set_doc.release (), set_list);
set->doc_allocated = 1;
if (set_func != NULL)
set->func = set_func;
show = add_set_or_show_cmd (name, show_cmd, theclass, var_type, args,
full_show_doc, show_list);
full_show_doc.release (), show_list);
show->doc_allocated = 1;
show->show_value_func = show_func;
/* Disable the default symbol completer. Doesn't make much sense

View file

@ -209,7 +209,7 @@ The flag -q disables the production of these headers and messages.%s"),
prefix, entity_kind, entity_kind, entity_kind,
(document_n_flag ? _("\n\
By default, the command will include non-debug symbols in the output;\n\
these can be excluded using the -n flag.") : ""));
these can be excluded using the -n flag.") : "")).release ();
}
/* See documentation in cli-utils.h. */

View file

@ -265,7 +265,6 @@ register_cp_abi (struct cp_abi_ops *abi)
void
set_cp_abi_as_auto_default (const char *short_name)
{
char *new_longname, *new_doc;
struct cp_abi_ops *abi = find_cp_abi (short_name);
if (abi == NULL)
@ -279,12 +278,10 @@ set_cp_abi_as_auto_default (const char *short_name)
auto_cp_abi = *abi;
auto_cp_abi.shortname = "auto";
new_longname = xstrprintf ("currently \"%s\"", abi->shortname);
auto_cp_abi.longname = new_longname;
new_doc = xstrprintf ("Automatically selected; currently \"%s\"",
abi->shortname);
auto_cp_abi.doc = new_doc;
auto_cp_abi.longname = xstrprintf ("currently \"%s\"",
abi->shortname).release ();
auto_cp_abi.doc = xstrprintf ("Automatically selected; currently \"%s\"",
abi->shortname).release ();
/* Since we copy the current ABI into current_cp_abi instead of
using a pointer, if auto is currently the default, we need to

View file

@ -182,11 +182,8 @@ new_variant (void)
in the G packet. If we need more in the future, we'll add them
elsewhere. */
for (r = acc0_regnum; r <= acc7_regnum; r++)
{
char *buf;
buf = xstrprintf ("acc%d", r - acc0_regnum);
var->register_names[r] = buf;
}
var->register_names[r]
= xstrprintf ("acc%d", r - acc0_regnum).release ();
/* accg0 - accg7: These are one byte registers. The remote protocol
provides the raw values packed four into a slot. accg0123 and
@ -195,11 +192,8 @@ new_variant (void)
likely not want to see these raw values. */
for (r = accg0_regnum; r <= accg7_regnum; r++)
{
char *buf;
buf = xstrprintf ("accg%d", r - accg0_regnum);
var->register_names[r] = buf;
}
var->register_names[r]
= xstrprintf ("accg%d", r - accg0_regnum).release ();
/* msr0 and msr1. */

View file

@ -136,7 +136,7 @@ gcore_command (const char *args, int from_tty)
else
{
/* Default corefile name is "core.PID". */
corefilename.reset (xstrprintf ("core.%d", inferior_ptid.pid ()));
corefilename = xstrprintf ("core.%d", inferior_ptid.pid ());
}
if (info_verbose)

View file

@ -968,7 +968,7 @@ gdbscm_set_breakpoint_stop_x (SCM self, SCM newvalue)
= xstrprintf (_("Only one stop condition allowed. There is"
" currently a %s stop condition defined for"
" this breakpoint."),
ext_lang_capitalized_name (extlang));
ext_lang_capitalized_name (extlang)).release ();
scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (error_text);

View file

@ -475,9 +475,7 @@ gdbscm_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, *msg;
char *msg;
/* Skip trailing whitespace. */
for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
@ -493,9 +491,9 @@ gdbscm_parse_command_name (const char *name,
/* 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)
@ -503,20 +501,19 @@ gdbscm_parse_command_name (const char *name,
if (i < 0)
{
*base_list = start_list;
return result;
return result.release ();
}
prefix_text = (char *) xmalloc (i + 2);
memcpy (prefix_text, name, i + 1);
prefix_text[i + 1] = '\0';
gdb::unique_xmalloc_ptr<char> prefix_text ((char *) xmalloc (i + 2));
memcpy (prefix_text.get (), name, i + 1);
prefix_text.get ()[i + 1] = '\0';
prefix_text2 = prefix_text;
const char *prefix_text2 = prefix_text.get ();
elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, NULL, 1);
if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
{
msg = xstrprintf (_("could not find command prefix '%s'"), prefix_text);
xfree (prefix_text);
xfree (result);
msg = xstrprintf (_("could not find command prefix '%s'"),
prefix_text.get ()).release ();
scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (msg);
gdbscm_out_of_range_error (func_name, arg_pos,
@ -525,14 +522,12 @@ gdbscm_parse_command_name (const char *name,
if (elt->is_prefix ())
{
xfree (prefix_text);
*base_list = elt->subcommands;
return result;
return result.release ();
}
msg = xstrprintf (_("'%s' is not a prefix command"), prefix_text);
xfree (prefix_text);
xfree (result);
msg = xstrprintf (_("'%s' is not a prefix command"),
prefix_text.get ()).release ();
scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (msg);
gdbscm_out_of_range_error (func_name, arg_pos,

View file

@ -234,7 +234,7 @@ SCM
gdbscm_make_type_error (const char *subr, int arg_pos, SCM bad_value,
const char *expected_type)
{
char *msg;
gdb::unique_xmalloc_ptr<char> msg;
SCM result;
if (arg_pos > 0)
@ -262,9 +262,8 @@ gdbscm_make_type_error (const char *subr, int arg_pos, SCM bad_value,
msg = xstrprintf (_("Wrong type argument: ~S"));
}
result = gdbscm_make_error (scm_arg_type_key, subr, msg,
result = gdbscm_make_error (scm_arg_type_key, subr, msg.get (),
scm_list_1 (bad_value), scm_list_1 (bad_value));
xfree (msg);
return result;
}
@ -279,7 +278,7 @@ static SCM
gdbscm_make_arg_error (SCM key, const char *subr, int arg_pos, SCM bad_value,
const char *error_prefix, const char *error)
{
char *msg;
gdb::unique_xmalloc_ptr<char> msg;
SCM result;
if (error_prefix != NULL)
@ -300,9 +299,8 @@ gdbscm_make_arg_error (SCM key, const char *subr, int arg_pos, SCM bad_value,
msg = xstrprintf (_("%s: ~S"), error);
}
result = gdbscm_make_error (key, subr, msg,
scm_list_1 (bad_value), scm_list_1 (bad_value));
xfree (msg);
result = gdbscm_make_error (key, subr, msg.get (), scm_list_1 (bad_value),
scm_list_1 (bad_value));
return result;
}

View file

@ -191,16 +191,13 @@ gdbscm_gsmob_kind (SCM self)
SCM smob, result;
scm_t_bits smobnum;
const char *name;
char *kind;
smob = gsscm_get_gsmob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
smobnum = SCM_SMOBNUM (smob);
name = SCM_SMOBNAME (smobnum);
kind = xstrprintf ("<%s>", name);
result = scm_from_latin1_symbol (kind);
xfree (kind);
gdb::unique_xmalloc_ptr<char> kind = xstrprintf ("<%s>", name);
result = scm_from_latin1_symbol (kind.get ());
return result;
}

View file

@ -139,7 +139,7 @@ static const unsigned min_memory_port_buf_size = 1;
static const unsigned max_memory_port_buf_size = 4096;
/* "out of range" error message for buf sizes. */
static char *out_of_range_buf_size;
static gdb::unique_xmalloc_ptr<char> out_of_range_buf_size;
#else
@ -1447,7 +1447,7 @@ gdbscm_set_memory_port_read_buffer_size_x (SCM port, SCM size)
max_memory_port_buf_size))
{
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2, size,
out_of_range_buf_size);
out_of_range_buf_size.get ());
}
iomem = (ioscm_memory_port *) SCM_STREAM (port);
@ -1497,7 +1497,7 @@ gdbscm_set_memory_port_write_buffer_size_x (SCM port, SCM size)
max_memory_port_buf_size))
{
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2, size,
out_of_range_buf_size);
out_of_range_buf_size.get ());
}
iomem = (ioscm_memory_port *) SCM_STREAM (port);

View file

@ -164,8 +164,8 @@ jit_reader_load_command (const char *args, int from_tty)
error (_("JIT reader already loaded. Run jit-reader-unload first."));
if (!IS_ABSOLUTE_PATH (file.get ()))
file.reset (xstrprintf ("%s%s%s", jit_reader_dir.c_str (), SLASH_STRING,
file.get ()));
file = xstrprintf ("%s%s%s", jit_reader_dir.c_str (),
SLASH_STRING, file.get ());
loaded_jit_reader = jit_reader_load (file.get ());
reinit_frame_cache ();

View file

@ -602,8 +602,7 @@ language_defn::watch_location_expression (struct type *type,
/* Generates an expression that assumes a C like syntax is valid. */
type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
std::string name = type_to_string (type);
return gdb::unique_xmalloc_ptr<char>
(xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr)));
return xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr));
}
/* See language.h. */

View file

@ -1078,11 +1078,12 @@ add_sal_to_sals (struct linespec_state *self,
the time being. */
if (symname != NULL && sal->line != 0
&& self->language->la_language == language_ada)
canonical->suffix = xstrprintf ("%s:%d", symname, sal->line);
canonical->suffix = xstrprintf ("%s:%d", symname,
sal->line).release ();
else if (symname != NULL)
canonical->suffix = xstrdup (symname);
else
canonical->suffix = xstrprintf ("%d", sal->line);
canonical->suffix = xstrprintf ("%d", sal->line).release ();
canonical->symtab = sal->symtab;
}
else

View file

@ -415,9 +415,12 @@ event_location_to_string (struct event_location *location)
break;
case ADDRESS_LOCATION:
{
const char *addr_string
= core_addr_to_string (EL_ADDRESS (location));
EL_STRING (location)
= xstrprintf ("*%s",
core_addr_to_string (EL_ADDRESS (location)));
= xstrprintf ("*%s", addr_string).release ();
}
break;
case EXPLICIT_LOCATION:

View file

@ -893,7 +893,7 @@ fixup_definition (const char *filename, int line, struct macro_definition *def)
}
else if (def->argc == macro_LINE)
{
saved_expansion.reset (xstrprintf ("%d", line));
saved_expansion = xstrprintf ("%d", line);
def->replacement = saved_expansion.get ();
}
}

View file

@ -702,7 +702,7 @@ captured_main_1 (struct captured_main_args *context)
/* Prefix warning messages with the command name. */
gdb::unique_xmalloc_ptr<char> tmp_warn_preprint
(xstrprintf ("%s: warning: ", gdb_program_name));
= xstrprintf ("%s: warning: ", gdb_program_name);
warning_pre_print = tmp_warn_preprint.get ();
current_directory = getcwd (NULL, 0);

View file

@ -48,7 +48,7 @@ env_execute_cli_command (const char *cmd, const char *args)
gdb::unique_xmalloc_ptr<char> run;
if (args != NULL)
run.reset (xstrprintf ("%s %s", cmd, args));
run = xstrprintf ("%s %s", cmd, args);
else
run.reset (xstrdup (cmd));
execute_command ( /*ui */ run.get (), 0 /*from_tty */ );

View file

@ -781,7 +781,7 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
if (lineobj != NULL)
{
if (PyInt_Check (lineobj))
line.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj)));
line = xstrprintf ("%ld", PyInt_AsLong (lineobj));
else if (PyString_Check (lineobj))
line = python_string_to_host_string (lineobj);
else

View file

@ -1919,41 +1919,36 @@ static void
add_packet_config_cmd (struct packet_config *config, const char *name,
const char *title, int legacy)
{
char *set_doc;
char *show_doc;
char *cmd_name;
config->name = name;
config->title = title;
set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
gdb::unique_xmalloc_ptr<char> set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
name, title);
show_doc = xstrprintf ("Show current use of remote "
"protocol `%s' (%s) packet.",
gdb::unique_xmalloc_ptr<char> show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
name, title);
/* set/show TITLE-packet {auto,on,off} */
cmd_name = xstrprintf ("%s-packet", title);
gdb::unique_xmalloc_ptr<char> cmd_name = xstrprintf ("%s-packet", title);
set_show_commands cmds
= add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
&config->detect, set_doc,
show_doc, NULL, /* help_doc */
= add_setshow_auto_boolean_cmd (cmd_name.release (), class_obscure,
&config->detect, set_doc.get (),
show_doc.get (), NULL, /* help_doc */
NULL,
show_remote_protocol_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist);
config->show_cmd = cmds.show;
/* The command code copies the documentation strings. */
xfree (set_doc);
xfree (show_doc);
/* set/show remote NAME-packet {auto,on,off} -- legacy. */
if (legacy)
{
char *legacy_name;
legacy_name = xstrprintf ("%s-packet", name);
add_alias_cmd (legacy_name, cmds.set, class_obscure, 0,
/* It's not clear who should take ownership of this string, so, for
now, make it static, and give copies to each of the add_alias_cmd
calls below. */
static gdb::unique_xmalloc_ptr<char> legacy_name
= xstrprintf ("%s-packet", name);
add_alias_cmd (legacy_name.get (), cmds.set, class_obscure, 0,
&remote_set_cmdlist);
add_alias_cmd (legacy_name, cmds.show, class_obscure, 0,
add_alias_cmd (legacy_name.get (), cmds.show, class_obscure, 0,
&remote_show_cmdlist);
}
}

View file

@ -566,8 +566,8 @@ private:
for (auto &reg : m_registers)
{
int csr_num = reg.regnum - RISCV_FIRST_CSR_REGNUM;
const char *alias = xstrprintf ("csr%d", csr_num);
reg.names.push_back (alias);
gdb::unique_xmalloc_ptr<char> alias = xstrprintf ("csr%d", csr_num);
reg.names.push_back (alias.release ());
}
}
};

View file

@ -113,9 +113,8 @@ public:
{
type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
std::string name = type_to_string (type);
return gdb::unique_xmalloc_ptr<char>
(xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
name.c_str ()));
return xstrprintf ("*(%s as *mut %s)", core_addr_to_string (addr),
name.c_str ());
}
/* See language.h. */

View file

@ -650,7 +650,7 @@ complete_skip_number (cmd_list_element *cmd,
for (const skiplist_entry &entry : skiplist_entries)
{
gdb::unique_xmalloc_ptr<char> name (xstrprintf ("%d", entry.number ()));
gdb::unique_xmalloc_ptr<char> name = xstrprintf ("%d", entry.number ());
if (strncmp (word, name.get (), word_len) == 0)
completer.add_completion (std::move (name));
}

View file

@ -887,15 +887,15 @@ void
add_deprecated_target_alias (const target_info &tinfo, const char *alias)
{
struct cmd_list_element *c;
char *alt;
/* If we use add_alias_cmd, here, we do not get the deprecated warning,
see PR cli/15104. */
c = add_cmd (alias, no_class, tinfo.doc, &targetlist);
c->func = open_target;
c->set_context ((void *) &tinfo);
alt = xstrprintf ("target %s", tinfo.shortname);
deprecate_cmd (c, alt);
gdb::unique_xmalloc_ptr<char> alt
= xstrprintf ("target %s", tinfo.shortname);
deprecate_cmd (c, alt.release ());
}
/* Stub functions */

View file

@ -2809,7 +2809,7 @@ all_tracepoint_actions (struct breakpoint *t)
if (!default_collect.empty ())
{
gdb::unique_xmalloc_ptr<char> default_collect_line
(xstrprintf ("collect %s", default_collect.c_str ()));
= xstrprintf ("collect %s", default_collect.c_str ());
validate_actionline (default_collect_line.get (), t);
actions.reset (new struct command_line (simple_control,

View file

@ -850,10 +850,10 @@ add_layout_command (const char *name, tui_layout_split *layout)
layout->specification (&spec, 0);
gdb::unique_xmalloc_ptr<char> doc
(xstrprintf (_("Apply the \"%s\" layout.\n\
= xstrprintf (_("Apply the \"%s\" layout.\n\
This layout was created using:\n\
tui new-layout %s %s"),
name, name, spec.c_str ()));
name, name, spec.c_str ());
cmd = add_cmd (name, class_tui, nullptr, doc.get (), &layout_list);
cmd->set_context (layout);

View file

@ -534,10 +534,10 @@ add_internal_problem_command (struct internal_problem *problem)
set_doc and show_doc in this function. */
const char *set_doc
= xstrprintf (_("Configure what GDB does when %s is detected."),
problem->name);
problem->name).release ();
const char *show_doc
= xstrprintf (_("Show what GDB does when %s is detected."),
problem->name);
problem->name).release ();
add_setshow_prefix_cmd (problem->name, class_maintenance,
set_doc, show_doc, set_cmd_list, show_cmd_list,

View file

@ -727,7 +727,8 @@ xtensa_init_reggroups (void)
xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
xtensa_cp[i] = reggroup_new (xstrprintf ("cp%d", i), USER_REGGROUP);
xtensa_cp[i] = reggroup_new (xstrprintf ("cp%d", i).release (),
USER_REGGROUP);
}
static void

View file

@ -32,19 +32,18 @@ xzalloc (size_t size)
/* Like asprintf/vasprintf but get an internal_error if the call
fails. */
char *
gdb::unique_xmalloc_ptr<char>
xstrprintf (const char *format, ...)
{
char *ret;
va_list args;
va_start (args, format);
ret = xstrvprintf (format, args);
gdb::unique_xmalloc_ptr<char> ret = xstrvprintf (format, args);
va_end (args);
return ret;
}
char *
gdb::unique_xmalloc_ptr<char>
xstrvprintf (const char *format, va_list ap)
{
char *ret = NULL;
@ -56,7 +55,7 @@ xstrvprintf (const char *format, va_list ap)
happen, but just to be sure. */
if (ret == NULL || status < 0)
internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
return ret;
return gdb::unique_xmalloc_ptr<char> (ret);
}
int

View file

@ -23,6 +23,7 @@
#include <string>
#include <vector>
#include "gdbsupport/byte-vector.h"
#include "gdbsupport/gdb_unique_ptr.h"
#include "poison.h"
@ -54,8 +55,9 @@ void *xzalloc (size_t);
/* Like asprintf and vasprintf, but return the string, throw an error
if no memory. */
char *xstrprintf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
char *xstrvprintf (const char *format, va_list ap)
gdb::unique_xmalloc_ptr<char> xstrprintf (const char *format, ...)
ATTRIBUTE_PRINTF (1, 2);
gdb::unique_xmalloc_ptr<char> xstrvprintf (const char *format, va_list ap)
ATTRIBUTE_PRINTF (1, 0);
/* Like snprintf, but throw an error if the output buffer is too small. */