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

View file

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

View file

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

View file

@ -960,35 +960,31 @@ intermediate_encoding (void)
{ {
iconv_t desc; iconv_t desc;
static const char *stored_result = NULL; static const char *stored_result = NULL;
char *result; gdb::unique_xmalloc_ptr<char> result;
if (stored_result) if (stored_result)
return stored_result; return stored_result;
result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8), result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8),
ENDIAN_SUFFIX); ENDIAN_SUFFIX);
/* Check that the name is supported by iconv_open. */ /* 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) if (desc != (iconv_t) -1)
{ {
iconv_close (desc); iconv_close (desc);
stored_result = result; stored_result = result.release ();
return result; return stored_result;
} }
/* Not valid, free the allocated memory. */
xfree (result);
/* Second try, with UCS-2 type. */ /* Second try, with UCS-2 type. */
result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t), result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t),
ENDIAN_SUFFIX); ENDIAN_SUFFIX);
/* Check that the name is supported by iconv_open. */ /* 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) if (desc != (iconv_t) -1)
{ {
iconv_close (desc); iconv_close (desc);
stored_result = result; stored_result = result.release ();
return result; return stored_result;
} }
/* Not valid, free the allocated memory. */
xfree (result);
/* No valid charset found, generate error here. */ /* No valid charset found, generate error here. */
error (_("Unable to find a valid charset for string conversions")); 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 symtab_and_line sal;
struct symbol *sym; struct symbol *sym;
const char *editor; const char *editor;
char *p;
const char *fn; const char *fn;
/* Pull in the current default source line if necessary. */ /* 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 /* Quote the file name, in case it has whitespace or other special
characters. */ characters. */
p = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn); gdb::unique_xmalloc_ptr<char> p
shell_escape (p, from_tty); = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
xfree (p); shell_escape (p.get (), from_tty);
} }
/* The options for the "pipe" command. */ /* 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\ -v: each command in FILE is echoed as it is executed.\n\
\n\ \n\
Note that the file \"%s\" is read automatically in this way\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, c = add_cmd ("source", class_support, source_command,
source_help_text, &cmdlist); source_help_text, &cmdlist);
set_cmd_completer (c, filename_completer); 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 *set;
struct cmd_list_element *show; struct cmd_list_element *show;
char *full_set_doc; gdb::unique_xmalloc_ptr<char> full_set_doc;
char *full_show_doc; gdb::unique_xmalloc_ptr<char> full_show_doc;
if (help_doc != NULL) if (help_doc != NULL)
{ {
@ -544,18 +544,18 @@ add_setshow_cmd_full_erased (const char *name,
} }
else else
{ {
full_set_doc = xstrdup (set_doc); full_set_doc = make_unique_xstrdup (set_doc);
full_show_doc = xstrdup (show_doc); full_show_doc = make_unique_xstrdup (show_doc);
} }
set = add_set_or_show_cmd (name, set_cmd, theclass, var_type, args, 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; set->doc_allocated = 1;
if (set_func != NULL) if (set_func != NULL)
set->func = set_func; set->func = set_func;
show = add_set_or_show_cmd (name, show_cmd, theclass, var_type, args, 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->doc_allocated = 1;
show->show_value_func = show_func; show->show_value_func = show_func;
/* Disable the default symbol completer. Doesn't make much sense /* 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, prefix, entity_kind, entity_kind, entity_kind,
(document_n_flag ? _("\n\ (document_n_flag ? _("\n\
By default, the command will include non-debug symbols in the output;\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. */ /* See documentation in cli-utils.h. */

View file

@ -265,7 +265,6 @@ register_cp_abi (struct cp_abi_ops *abi)
void void
set_cp_abi_as_auto_default (const char *short_name) 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); struct cp_abi_ops *abi = find_cp_abi (short_name);
if (abi == NULL) if (abi == NULL)
@ -279,12 +278,10 @@ set_cp_abi_as_auto_default (const char *short_name)
auto_cp_abi = *abi; auto_cp_abi = *abi;
auto_cp_abi.shortname = "auto"; auto_cp_abi.shortname = "auto";
new_longname = xstrprintf ("currently \"%s\"", abi->shortname); auto_cp_abi.longname = xstrprintf ("currently \"%s\"",
auto_cp_abi.longname = new_longname; abi->shortname).release ();
auto_cp_abi.doc = xstrprintf ("Automatically selected; currently \"%s\"",
new_doc = xstrprintf ("Automatically selected; currently \"%s\"", abi->shortname).release ();
abi->shortname);
auto_cp_abi.doc = new_doc;
/* Since we copy the current ABI into current_cp_abi instead of /* Since we copy the current ABI into current_cp_abi instead of
using a pointer, if auto is currently the default, we need to 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 in the G packet. If we need more in the future, we'll add them
elsewhere. */ elsewhere. */
for (r = acc0_regnum; r <= acc7_regnum; r++) for (r = acc0_regnum; r <= acc7_regnum; r++)
{ var->register_names[r]
char *buf; = xstrprintf ("acc%d", r - acc0_regnum).release ();
buf = xstrprintf ("acc%d", r - acc0_regnum);
var->register_names[r] = buf;
}
/* accg0 - accg7: These are one byte registers. The remote protocol /* accg0 - accg7: These are one byte registers. The remote protocol
provides the raw values packed four into a slot. accg0123 and 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. */ likely not want to see these raw values. */
for (r = accg0_regnum; r <= accg7_regnum; r++) for (r = accg0_regnum; r <= accg7_regnum; r++)
{ var->register_names[r]
char *buf; = xstrprintf ("accg%d", r - accg0_regnum).release ();
buf = xstrprintf ("accg%d", r - accg0_regnum);
var->register_names[r] = buf;
}
/* msr0 and msr1. */ /* msr0 and msr1. */

View file

@ -136,7 +136,7 @@ gcore_command (const char *args, int from_tty)
else else
{ {
/* Default corefile name is "core.PID". */ /* Default corefile name is "core.PID". */
corefilename.reset (xstrprintf ("core.%d", inferior_ptid.pid ())); corefilename = xstrprintf ("core.%d", inferior_ptid.pid ());
} }
if (info_verbose) 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" = xstrprintf (_("Only one stop condition allowed. There is"
" currently a %s stop condition defined for" " currently a %s stop condition defined for"
" this breakpoint."), " this breakpoint."),
ext_lang_capitalized_name (extlang)); ext_lang_capitalized_name (extlang)).release ();
scm_dynwind_begin ((scm_t_dynwind_flags) 0); scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (error_text); gdbscm_dynwind_xfree (error_text);

View file

@ -475,9 +475,7 @@ gdbscm_parse_command_name (const char *name,
struct cmd_list_element *elt; struct cmd_list_element *elt;
int len = strlen (name); int len = strlen (name);
int i, lastchar; int i, lastchar;
char *prefix_text; char *msg;
const char *prefix_text2;
char *result, *msg;
/* Skip trailing whitespace. */ /* Skip trailing whitespace. */
for (i = len - 1; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i) 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. */ /* Find first character of the final word. */
for (; i > 0 && valid_cmd_char_p (name[i - 1]); --i) for (; i > 0 && valid_cmd_char_p (name[i - 1]); --i)
; ;
result = (char *) xmalloc (lastchar - i + 2); gdb::unique_xmalloc_ptr<char> result ((char *) xmalloc (lastchar - i + 2));
memcpy (result, &name[i], lastchar - i + 1); memcpy (result.get (), &name[i], lastchar - i + 1);
result[lastchar - i + 1] = '\0'; result.get ()[lastchar - i + 1] = '\0';
/* Skip whitespace again. */ /* Skip whitespace again. */
for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i) for (--i; i >= 0 && (name[i] == ' ' || name[i] == '\t'); --i)
@ -503,20 +501,19 @@ gdbscm_parse_command_name (const char *name,
if (i < 0) if (i < 0)
{ {
*base_list = start_list; *base_list = start_list;
return result; return result.release ();
} }
prefix_text = (char *) xmalloc (i + 2); gdb::unique_xmalloc_ptr<char> prefix_text ((char *) xmalloc (i + 2));
memcpy (prefix_text, name, i + 1); memcpy (prefix_text.get (), name, i + 1);
prefix_text[i + 1] = '\0'; 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); elt = lookup_cmd_1 (&prefix_text2, *start_list, NULL, NULL, 1);
if (elt == NULL || elt == CMD_LIST_AMBIGUOUS) if (elt == NULL || elt == CMD_LIST_AMBIGUOUS)
{ {
msg = xstrprintf (_("could not find command prefix '%s'"), prefix_text); msg = xstrprintf (_("could not find command prefix '%s'"),
xfree (prefix_text); prefix_text.get ()).release ();
xfree (result);
scm_dynwind_begin ((scm_t_dynwind_flags) 0); scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (msg); gdbscm_dynwind_xfree (msg);
gdbscm_out_of_range_error (func_name, arg_pos, gdbscm_out_of_range_error (func_name, arg_pos,
@ -525,14 +522,12 @@ gdbscm_parse_command_name (const char *name,
if (elt->is_prefix ()) if (elt->is_prefix ())
{ {
xfree (prefix_text);
*base_list = elt->subcommands; *base_list = elt->subcommands;
return result; return result.release ();
} }
msg = xstrprintf (_("'%s' is not a prefix command"), prefix_text); msg = xstrprintf (_("'%s' is not a prefix command"),
xfree (prefix_text); prefix_text.get ()).release ();
xfree (result);
scm_dynwind_begin ((scm_t_dynwind_flags) 0); scm_dynwind_begin ((scm_t_dynwind_flags) 0);
gdbscm_dynwind_xfree (msg); gdbscm_dynwind_xfree (msg);
gdbscm_out_of_range_error (func_name, arg_pos, 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, gdbscm_make_type_error (const char *subr, int arg_pos, SCM bad_value,
const char *expected_type) const char *expected_type)
{ {
char *msg; gdb::unique_xmalloc_ptr<char> msg;
SCM result; SCM result;
if (arg_pos > 0) 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")); 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)); scm_list_1 (bad_value), scm_list_1 (bad_value));
xfree (msg);
return result; return result;
} }
@ -279,7 +278,7 @@ static SCM
gdbscm_make_arg_error (SCM key, const char *subr, int arg_pos, SCM bad_value, gdbscm_make_arg_error (SCM key, const char *subr, int arg_pos, SCM bad_value,
const char *error_prefix, const char *error) const char *error_prefix, const char *error)
{ {
char *msg; gdb::unique_xmalloc_ptr<char> msg;
SCM result; SCM result;
if (error_prefix != NULL) 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); msg = xstrprintf (_("%s: ~S"), error);
} }
result = gdbscm_make_error (key, subr, msg, result = gdbscm_make_error (key, subr, msg.get (), scm_list_1 (bad_value),
scm_list_1 (bad_value), scm_list_1 (bad_value)); scm_list_1 (bad_value));
xfree (msg);
return result; return result;
} }

View file

@ -191,16 +191,13 @@ gdbscm_gsmob_kind (SCM self)
SCM smob, result; SCM smob, result;
scm_t_bits smobnum; scm_t_bits smobnum;
const char *name; const char *name;
char *kind;
smob = gsscm_get_gsmob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); smob = gsscm_get_gsmob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
smobnum = SCM_SMOBNUM (smob); smobnum = SCM_SMOBNUM (smob);
name = SCM_SMOBNAME (smobnum); name = SCM_SMOBNAME (smobnum);
kind = xstrprintf ("<%s>", name); gdb::unique_xmalloc_ptr<char> kind = xstrprintf ("<%s>", name);
result = scm_from_latin1_symbol (kind); result = scm_from_latin1_symbol (kind.get ());
xfree (kind);
return result; 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; static const unsigned max_memory_port_buf_size = 4096;
/* "out of range" error message for buf sizes. */ /* "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 #else
@ -1447,7 +1447,7 @@ gdbscm_set_memory_port_read_buffer_size_x (SCM port, SCM size)
max_memory_port_buf_size)) max_memory_port_buf_size))
{ {
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2, 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); 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)) max_memory_port_buf_size))
{ {
gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG2, 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); 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.")); error (_("JIT reader already loaded. Run jit-reader-unload first."));
if (!IS_ABSOLUTE_PATH (file.get ())) if (!IS_ABSOLUTE_PATH (file.get ()))
file.reset (xstrprintf ("%s%s%s", jit_reader_dir.c_str (), SLASH_STRING, file = xstrprintf ("%s%s%s", jit_reader_dir.c_str (),
file.get ())); SLASH_STRING, file.get ());
loaded_jit_reader = jit_reader_load (file.get ()); loaded_jit_reader = jit_reader_load (file.get ());
reinit_frame_cache (); 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. */ /* Generates an expression that assumes a C like syntax is valid. */
type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type))); type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
std::string name = type_to_string (type); std::string name = type_to_string (type);
return gdb::unique_xmalloc_ptr<char> return xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr));
(xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr)));
} }
/* See language.h. */ /* See language.h. */

View file

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

View file

@ -415,9 +415,12 @@ event_location_to_string (struct event_location *location)
break; break;
case ADDRESS_LOCATION: case ADDRESS_LOCATION:
EL_STRING (location) {
= xstrprintf ("*%s", const char *addr_string
core_addr_to_string (EL_ADDRESS (location))); = core_addr_to_string (EL_ADDRESS (location));
EL_STRING (location)
= xstrprintf ("*%s", addr_string).release ();
}
break; break;
case EXPLICIT_LOCATION: 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) else if (def->argc == macro_LINE)
{ {
saved_expansion.reset (xstrprintf ("%d", line)); saved_expansion = xstrprintf ("%d", line);
def->replacement = saved_expansion.get (); 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. */ /* Prefix warning messages with the command name. */
gdb::unique_xmalloc_ptr<char> tmp_warn_preprint 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 (); warning_pre_print = tmp_warn_preprint.get ();
current_directory = getcwd (NULL, 0); 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; gdb::unique_xmalloc_ptr<char> run;
if (args != NULL) if (args != NULL)
run.reset (xstrprintf ("%s %s", cmd, args)); run = xstrprintf ("%s %s", cmd, args);
else else
run.reset (xstrdup (cmd)); run.reset (xstrdup (cmd));
execute_command ( /*ui */ run.get (), 0 /*from_tty */ ); 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 (lineobj != NULL)
{ {
if (PyInt_Check (lineobj)) if (PyInt_Check (lineobj))
line.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj))); line = xstrprintf ("%ld", PyInt_AsLong (lineobj));
else if (PyString_Check (lineobj)) else if (PyString_Check (lineobj))
line = python_string_to_host_string (lineobj); line = python_string_to_host_string (lineobj);
else else

View file

@ -1919,41 +1919,36 @@ static void
add_packet_config_cmd (struct packet_config *config, const char *name, add_packet_config_cmd (struct packet_config *config, const char *name,
const char *title, int legacy) const char *title, int legacy)
{ {
char *set_doc;
char *show_doc;
char *cmd_name;
config->name = name; config->name = name;
config->title = title; config->title = title;
set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet.", gdb::unique_xmalloc_ptr<char> set_doc
name, title); = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
show_doc = xstrprintf ("Show current use of remote " name, title);
"protocol `%s' (%s) packet.", gdb::unique_xmalloc_ptr<char> show_doc
name, title); = xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
name, title);
/* set/show TITLE-packet {auto,on,off} */ /* 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 set_show_commands cmds
= add_setshow_auto_boolean_cmd (cmd_name, class_obscure, = add_setshow_auto_boolean_cmd (cmd_name.release (), class_obscure,
&config->detect, set_doc, &config->detect, set_doc.get (),
show_doc, NULL, /* help_doc */ show_doc.get (), NULL, /* help_doc */
NULL, NULL,
show_remote_protocol_packet_cmd, show_remote_protocol_packet_cmd,
&remote_set_cmdlist, &remote_show_cmdlist); &remote_set_cmdlist, &remote_show_cmdlist);
config->show_cmd = cmds.show; 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. */ /* set/show remote NAME-packet {auto,on,off} -- legacy. */
if (legacy) if (legacy)
{ {
char *legacy_name; /* 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
legacy_name = xstrprintf ("%s-packet", name); calls below. */
add_alias_cmd (legacy_name, cmds.set, class_obscure, 0, 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); &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); &remote_show_cmdlist);
} }
} }

View file

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

View file

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

View file

@ -650,7 +650,7 @@ complete_skip_number (cmd_list_element *cmd,
for (const skiplist_entry &entry : skiplist_entries) 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) if (strncmp (word, name.get (), word_len) == 0)
completer.add_completion (std::move (name)); completer.add_completion (std::move (name));
} }

View file

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

View file

@ -2809,7 +2809,7 @@ all_tracepoint_actions (struct breakpoint *t)
if (!default_collect.empty ()) if (!default_collect.empty ())
{ {
gdb::unique_xmalloc_ptr<char> default_collect_line 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); validate_actionline (default_collect_line.get (), t);
actions.reset (new struct command_line (simple_control, 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); layout->specification (&spec, 0);
gdb::unique_xmalloc_ptr<char> doc gdb::unique_xmalloc_ptr<char> doc
(xstrprintf (_("Apply the \"%s\" layout.\n\ = xstrprintf (_("Apply the \"%s\" layout.\n\
This layout was created using:\n\ This layout was created using:\n\
tui new-layout %s %s"), 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 = add_cmd (name, class_tui, nullptr, doc.get (), &layout_list);
cmd->set_context (layout); 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. */ set_doc and show_doc in this function. */
const char *set_doc const char *set_doc
= xstrprintf (_("Configure what GDB does when %s is detected."), = xstrprintf (_("Configure what GDB does when %s is detected."),
problem->name); problem->name).release ();
const char *show_doc const char *show_doc
= xstrprintf (_("Show what GDB does when %s is detected."), = xstrprintf (_("Show what GDB does when %s is detected."),
problem->name); problem->name).release ();
add_setshow_prefix_cmd (problem->name, class_maintenance, add_setshow_prefix_cmd (problem->name, class_maintenance,
set_doc, show_doc, set_cmd_list, show_cmd_list, 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); xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++) 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 static void

View file

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

View file

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