diff options
author | Andrew Burgess <aburgess@redhat.com> | 2021-11-08 14:58:46 +0000 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2021-11-16 17:45:45 +0000 |
commit | 8579fd136a614985bd27f20539c7bb7c5a51287d (patch) | |
tree | fb84850409a44e13e832cbadc9025d40c1d33d9f /gdb/guile/scm-cmd.c | |
parent | 2bb7589ddf61e163f2e414e7033fad56ea17e784 (diff) | |
download | binutils-8579fd136a614985bd27f20539c7bb7c5a51287d.zip binutils-8579fd136a614985bd27f20539c7bb7c5a51287d.tar.gz binutils-8579fd136a614985bd27f20539c7bb7c5a51287d.tar.bz2 |
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.
Diffstat (limited to 'gdb/guile/scm-cmd.c')
-rw-r--r-- | gdb/guile/scm-cmd.c | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/gdb/guile/scm-cmd.c b/gdb/guile/scm-cmd.c index 6764285..619c89c 100644 --- a/gdb/guile/scm-cmd.c +++ b/gdb/guile/scm-cmd.c @@ -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, |