Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 3/3] Remove alloca from lookup_cmd
Date: Thu, 22 Jan 2026 13:49:13 -0700	[thread overview]
Message-ID: <20260122-command-no-alloca-v1-3-402aa429bbbf@adacore.com> (raw)
In-Reply-To: <20260122-command-no-alloca-v1-0-402aa429bbbf@adacore.com>

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.
---
 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;

-- 
2.52.0


  parent reply	other threads:[~2026-01-22 20:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-22 20:49 [PATCH 0/3] Remove some uses of alloca from cli/ Tom Tromey
2026-01-22 20:49 ` [PATCH 1/3] Use string_view in cli-decode.c:find_cmd Tom Tromey
2026-01-22 20:49 ` [PATCH 2/3] Don't make copies when calling find_cmd Tom Tromey
2026-01-22 20:49 ` Tom Tromey [this message]
2026-01-29 19:07 ` [PATCH 0/3] Remove some uses of alloca from cli/ Kevin Buettner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260122-command-no-alloca-v1-3-402aa429bbbf@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox