[binutils-gdb] Remove alloca from lookup_cmd

Tom Tromey tromey@sourceware.org
Thu Jan 29 19:47:17 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=68567c5479cee1fd7f94be3b71f7bce88014b22a

commit 68567c5479cee1fd7f94be3b71f7bce88014b22a
Author: Tom Tromey <tromey@adacore.com>
Date:   Thu Jan 22 13:31:12 2026 -0700

    Remove alloca from lookup_cmd
    
    lookup_cmd uses alloca to make a copy of a string, just for an error
    message.  However, it's just as easy to use "%.*s" (already used once
    in undef_cmd_error) and to pass in a string_view, avoiding the need
    for an alloca and a copy.
    
    Approved-By: Kevin Buettner <kevinb@redhat.com>

Diff:
---
 gdb/cli/cli-decode.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index f581f128a34..285f5f1f0c4 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -27,8 +27,6 @@
 
 /* Prototypes for local functions.  */
 
-static void undef_cmd_error (const char *, const char *);
-
 static cmd_list_element::aliases_list_type delete_cmd
   (const char *name, cmd_list_element **list, cmd_list_element **prehook,
    cmd_list_element **prehookee, cmd_list_element **posthook,
@@ -2369,11 +2367,11 @@ lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
 /* All this hair to move the space to the front of cmdtype */
 
 static void
-undef_cmd_error (const char *cmdtype, const char *q)
+undef_cmd_error (const char *cmdtype, std::string_view q)
 {
-  error (_("Undefined %scommand: \"%s\".  Try \"help%s%.*s\"."),
+  error (_("Undefined %scommand: \"%.*s\".  Try \"help%s%.*s\"."),
 	 cmdtype,
-	 q,
+	 (int) q.size (), q.data (),
 	 *cmdtype ? " " : "",
 	 (int) strlen (cmdtype) - 1,
 	 cmdtype);
@@ -2419,13 +2417,8 @@ lookup_cmd (const char **line, struct cmd_list_element *list,
     {
       if (!allow_unknown)
 	{
-	  char *q;
 	  int len = find_command_name_length (*line);
-
-	  q = (char *) alloca (len + 1);
-	  strncpy (q, *line, len);
-	  q[len] = '\0';
-	  undef_cmd_error (cmdtype, q);
+	  undef_cmd_error (cmdtype, std::string_view (*line, len));
 	}
       else
 	return 0;
@@ -2491,7 +2484,7 @@ lookup_cmd (const char **line, struct cmd_list_element *list,
       *line = skip_spaces (*line);
 
       if (c->is_prefix () && **line && !c->allow_unknown)
-	undef_cmd_error (c->prefixname ().c_str (), *line);
+	undef_cmd_error (c->prefixname ().c_str (), std::string_view (*line));
 
       /* Seems to be what he wants.  Return it.  */
       return c;


More information about the Gdb-cvs mailing list