]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Remove alloca from lookup_cmd
authorTom Tromey <tromey@adacore.com>
Thu, 22 Jan 2026 20:31:12 +0000 (13:31 -0700)
committerTom Tromey <tromey@adacore.com>
Thu, 29 Jan 2026 19:43:53 +0000 (12:43 -0700)
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>
gdb/cli/cli-decode.c

index f581f128a34f94cb7f682b3180da8761938fdf28..285f5f1f0c40583bf89d25f7fb45d54a11f73496 100644 (file)
@@ -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;