Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH 0/3] Remove some uses of alloca from cli/
@ 2026-01-22 20:49 Tom Tromey
  2026-01-22 20:49 ` [PATCH 1/3] Use string_view in cli-decode.c:find_cmd Tom Tromey
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Tom Tromey @ 2026-01-22 20:49 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

I found some spots in the CLI code that needlessly use alloca.  This
patch fixes these.

Regression tested on x86-64 Fedora 41.

Signed-off-by: Tom Tromey <tromey@adacore.com>
---
Tom Tromey (3):
      Use string_view in cli-decode.c:find_cmd
      Don't make copies when calling find_cmd
      Remove alloca from lookup_cmd

 gdb/cli/cli-decode.c | 50 +++++++++++++++-----------------------------------
 1 file changed, 15 insertions(+), 35 deletions(-)
---
base-commit: 44ae137c1fcefe486639b7eab3f6a7f60e4659e5
change-id: 20260122-command-no-alloca-f2410f3d5058

Best regards,
-- 
Tom Tromey <tromey@adacore.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] Use string_view in cli-decode.c:find_cmd
  2026-01-22 20:49 [PATCH 0/3] Remove some uses of alloca from cli/ Tom Tromey
@ 2026-01-22 20:49 ` Tom Tromey
  2026-01-22 20:49 ` [PATCH 2/3] Don't make copies when calling find_cmd Tom Tromey
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2026-01-22 20:49 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes cli-decode.c:find_cmd to use std::string_view.  I've made
this a separate patch to make the next patch more obvious.
---
 gdb/cli/cli-decode.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 04935575b67..c9bd610a3ac 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -34,12 +34,6 @@ static cmd_list_element::aliases_list_type delete_cmd
    cmd_list_element **prehookee, cmd_list_element **posthook,
    cmd_list_element **posthookee);
 
-static struct cmd_list_element *find_cmd (const char *command,
-					  int len,
-					  struct cmd_list_element *clist,
-					  int ignore_help_classes,
-					  int *nfound);
-
 static void help_cmd_list (struct cmd_list_element *list,
 			   command_classes theclass,
 			   bool recurse,
@@ -2174,7 +2168,7 @@ help_cmd_list (struct cmd_list_element *list, command_classes theclass,
    found in nfound.  */
 
 static struct cmd_list_element *
-find_cmd (const char *command, int len, struct cmd_list_element *clist,
+find_cmd (std::string_view command, struct cmd_list_element *clist,
 	  int ignore_help_classes, int *nfound)
 {
   struct cmd_list_element *found, *c;
@@ -2182,12 +2176,12 @@ find_cmd (const char *command, int len, struct cmd_list_element *clist,
   found = NULL;
   *nfound = 0;
   for (c = clist; c; c = c->next)
-    if (!strncmp (command, c->name, len)
+    if (startswith (c->name, command)
 	&& (!ignore_help_classes || !c->is_command_class_help ()))
       {
 	found = c;
 	(*nfound)++;
-	if (c->name[len] == '\0')
+	if (c->name[command.size ()] == '\0')
 	  {
 	    *nfound = 1;
 	    break;
@@ -2290,7 +2284,8 @@ lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
   /* Look it up.  */
   found = 0;
   nfound = 0;
-  found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
+  found = find_cmd (std::string_view (command, len),
+		    clist, ignore_help_classes, &nfound);
 
   /* If nothing matches, we have a simple failure.  */
   if (nfound == 0)
@@ -2676,7 +2671,7 @@ lookup_cmd_composition_1 (const char *text,
 
       /* Look it up.  */
       int nfound = 0;
-      *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
+      *cmd = find_cmd (command, cur_list, 1, &nfound);
 
       /* We only handle the case where a single command was found.  */
       if (nfound > 1)

-- 
2.52.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] Don't make copies when calling find_cmd
  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 ` Tom Tromey
  2026-01-22 20:49 ` [PATCH 3/3] Remove alloca from lookup_cmd Tom Tromey
  2026-01-29 19:07 ` [PATCH 0/3] Remove some uses of alloca from cli/ Kevin Buettner
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2026-01-22 20:49 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Currently both callers of find_cmd make a temporary string -- one with
std::string and one with the dreaded alloca.

However, as the previous change to find_cmd points out, these copies
are not needed.  Remove them and use a string_view instead.
---
 gdb/cli/cli-decode.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index c9bd610a3ac..f581f128a34 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -2258,7 +2258,6 @@ lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
 	      struct cmd_list_element **result_list, std::string *default_args,
 	      int ignore_help_classes, bool lookup_for_completion_p)
 {
-  char *command;
   int len, nfound;
   struct cmd_list_element *found, *c;
   bool found_alias = false;
@@ -2273,18 +2272,11 @@ lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
   if (len == 0)
     return 0;
 
-  /* *text and p now bracket the first command word to lookup (and
-     it's length is len).  We copy this into a local temporary.  */
-
-
-  command = (char *) alloca (len + 1);
-  memcpy (command, *text, len);
-  command[len] = '\0';
-
-  /* Look it up.  */
+  /* *TEXT is the first command word to lookup (and its length is
+     LEN).  Look it up.  */
   found = 0;
   nfound = 0;
-  found = find_cmd (std::string_view (command, len),
+  found = find_cmd (std::string_view (*text, len),
 		    clist, ignore_help_classes, &nfound);
 
   /* If nothing matches, we have a simple failure.  */
@@ -2666,8 +2658,8 @@ lookup_cmd_composition_1 (const char *text,
 	return 0;
 
       /* TEXT is the start of the first command word to lookup (and
-	 it's length is LEN).  We copy this into a local temporary.  */
-      std::string command (text, len);
+	 its length is LEN).  */
+      std::string_view command (text, len);
 
       /* Look it up.  */
       int nfound = 0;

-- 
2.52.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] Remove alloca from lookup_cmd
  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
  2026-01-29 19:07 ` [PATCH 0/3] Remove some uses of alloca from cli/ Kevin Buettner
  3 siblings, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2026-01-22 20:49 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/3] Remove some uses of alloca from cli/
  2026-01-22 20:49 [PATCH 0/3] Remove some uses of alloca from cli/ Tom Tromey
                   ` (2 preceding siblings ...)
  2026-01-22 20:49 ` [PATCH 3/3] Remove alloca from lookup_cmd Tom Tromey
@ 2026-01-29 19:07 ` Kevin Buettner
  3 siblings, 0 replies; 5+ messages in thread
From: Kevin Buettner @ 2026-01-29 19:07 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On Thu, 22 Jan 2026 13:49:10 -0700
Tom Tromey <tromey@adacore.com> wrote:

> I found some spots in the CLI code that needlessly use alloca.  This
> patch fixes these.
> 
> Regression tested on x86-64 Fedora 41.
> 
> Signed-off-by: Tom Tromey <tromey@adacore.com>
> ---
> Tom Tromey (3):
>       Use string_view in cli-decode.c:find_cmd
>       Don't make copies when calling find_cmd
>       Remove alloca from lookup_cmd

I've looked over all three patches.  All LGTM.

Approved-By: Kevin Buettner <kevinb@redhat.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-01-29 19:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 3/3] Remove alloca from lookup_cmd Tom Tromey
2026-01-29 19:07 ` [PATCH 0/3] Remove some uses of alloca from cli/ Kevin Buettner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox