public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Remove some alloca uses
@ 2024-04-17 22:52 Tom Tromey
  2024-04-20 20:27 ` John Baldwin
  2024-04-22  9:41 ` [PUSHED] gdb: fix unknown variable typo in c-exp.y Andrew Burgess
  0 siblings, 2 replies; 3+ messages in thread
From: Tom Tromey @ 2024-04-17 22:52 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

A few spots (mostly in the parsers) use alloca to ensure that a string
is terminated before passing it to a printf-like function (mostly
'error').  However, this isn't needed as the "%.*s" format can be used
instead.

This patch makes this change.

In one spot the alloca is dead code and is simply removed.

Regression tested on x86-64 Fedora 38.
---
 gdb/c-exp.y          | 19 ++++---------------
 gdb/cp-name-parser.y |  4 ----
 gdb/cp-support.c     | 12 ++----------
 gdb/d-exp.y          |  9 ++-------
 gdb/f-exp.y          |  9 ++-------
 gdb/go-exp.y         |  9 ++-------
 gdb/m2-exp.y         |  9 ++-------
 gdb/p-exp.y          |  9 ++-------
 8 files changed, 16 insertions(+), 64 deletions(-)

diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index 663c30f9517..87aca4d59b1 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -2784,13 +2784,8 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
 	toktype = parse_number (par_state, tokstart, p - tokstart,
 				got_dot | got_e | got_p, &yylval);
 	if (toktype == ERROR)
-	  {
-	    char *err_copy = (char *) alloca (p - tokstart + 1);
-
-	    memcpy (err_copy, tokstart, p - tokstart);
-	    err_copy[p - tokstart] = 0;
-	    error (_("Invalid number \"%s\"."), err_copy);
-	  }
+	  error (_("Invalid number \"%.*s\"."), (int) (p - tokstart),
+		 tokstart);
 	pstate->lexptr = p;
 	return toktype;
       }
@@ -3434,14 +3429,8 @@ c_print_token (FILE *file, int type, YYSTYPE value)
 
     case CHAR:
     case STRING:
-      {
-	char *copy = (char *) alloca (value.tsval.length + 1);
-
-	memcpy (copy, value.tsval.ptr, value.tsval.length);
-	copy[value.tsval.length] = '\0';
-
-	parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
-      }
+      parser_fprintf (file, "tsval<type=%d, %.*s>", value.tsval.type,
+		      value.tsval.length, val.tsval.ptr);
       break;
 
     case NSSTRING:
diff --git a/gdb/cp-name-parser.y b/gdb/cp-name-parser.y
index 87f13445bba..e944276d001 100644
--- a/gdb/cp-name-parser.y
+++ b/gdb/cp-name-parser.y
@@ -1702,10 +1702,6 @@ yylex (YYSTYPE *lvalp, cpname_state *state)
 				       lvalp);
 	if (toktype == ERROR)
 	  {
-	    char *err_copy = (char *) alloca (p - tokstart + 1);
-
-	    memcpy (err_copy, tokstart, p - tokstart);
-	    err_copy[p - tokstart] = 0;
 	    yyerror (state, _("invalid number"));
 	    return ERROR;
 	  }
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index e6e811ddf50..5fd53094d05 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -2214,19 +2214,11 @@ test_cp_remove_params ()
 static void
 first_component_command (const char *arg, int from_tty)
 {
-  int len;  
-  char *prefix; 
-
   if (!arg)
     return;
 
-  len = cp_find_first_component (arg);
-  prefix = (char *) alloca (len + 1);
-
-  memcpy (prefix, arg, len);
-  prefix[len] = '\0';
-
-  gdb_printf ("%s\n", prefix);
+  int len = cp_find_first_component (arg);
+  gdb_printf ("%.*s\n", len, arg);
 }
 
 /* Implement "info vtbl".  */
diff --git a/gdb/d-exp.y b/gdb/d-exp.y
index b2adad24d1a..13d2cfa44d8 100644
--- a/gdb/d-exp.y
+++ b/gdb/d-exp.y
@@ -1154,13 +1154,8 @@ lex_one_token (struct parser_state *par_state)
 	toktype = parse_number (par_state, tokstart, p - tokstart,
 				got_dot|got_e, &yylval);
 	if (toktype == ERROR)
-	  {
-	    char *err_copy = (char *) alloca (p - tokstart + 1);
-
-	    memcpy (err_copy, tokstart, p - tokstart);
-	    err_copy[p - tokstart] = 0;
-	    error (_("Invalid number \"%s\"."), err_copy);
-	  }
+	  error (_("Invalid number \"%.*s\"."), (int) (p - tokstart),
+		 tokstart);
 	pstate->lexptr = p;
 	return toktype;
       }
diff --git a/gdb/f-exp.y b/gdb/f-exp.y
index 11cd7948682..bdf9c32a81b 100644
--- a/gdb/f-exp.y
+++ b/gdb/f-exp.y
@@ -1557,13 +1557,8 @@ yylex (void)
 				got_dot|got_e|got_d,
 				&yylval);
 	if (toktype == ERROR)
-	  {
-	    char *err_copy = (char *) alloca (p - tokstart + 1);
-	    
-	    memcpy (err_copy, tokstart, p - tokstart);
-	    err_copy[p - tokstart] = 0;
-	    error (_("Invalid number \"%s\"."), err_copy);
-	  }
+	  error (_("Invalid number \"%.*s\"."), (int) (p - tokstart),
+		 tokstart);
 	pstate->lexptr = p;
 	return toktype;
       }
diff --git a/gdb/go-exp.y b/gdb/go-exp.y
index 20ab8ff76cf..1a6ebbe135b 100644
--- a/gdb/go-exp.y
+++ b/gdb/go-exp.y
@@ -1103,13 +1103,8 @@ lex_one_token (struct parser_state *par_state)
 	toktype = parse_number (par_state, tokstart, p - tokstart,
 				got_dot|got_e, &yylval);
 	if (toktype == ERROR)
-	  {
-	    char *err_copy = (char *) alloca (p - tokstart + 1);
-
-	    memcpy (err_copy, tokstart, p - tokstart);
-	    err_copy[p - tokstart] = 0;
-	    error (_("Invalid number \"%s\"."), err_copy);
-	  }
+	  error (_("Invalid number \"%.*s\"."), (int) (p - tokstart),
+		 tokstart);
 	par_state->lexptr = p;
 	return toktype;
       }
diff --git a/gdb/m2-exp.y b/gdb/m2-exp.y
index ebbc49c62a5..28005e1a700 100644
--- a/gdb/m2-exp.y
+++ b/gdb/m2-exp.y
@@ -869,13 +869,8 @@ yylex (void)
 	}
 	toktype = parse_number (p - tokstart);
 	if (toktype == ERROR)
-	  {
-	    char *err_copy = (char *) alloca (p - tokstart + 1);
-
-	    memcpy (err_copy, tokstart, p - tokstart);
-	    err_copy[p - tokstart] = 0;
-	    error (_("Invalid number \"%s\"."), err_copy);
-	  }
+	  error (_("Invalid number \"%.*s\"."), (int) (p - tokstart),
+		 tokstart);
 	pstate->lexptr = p;
 	return toktype;
     }
diff --git a/gdb/p-exp.y b/gdb/p-exp.y
index 2140b609225..f334db6b523 100644
--- a/gdb/p-exp.y
+++ b/gdb/p-exp.y
@@ -1239,13 +1239,8 @@ yylex (void)
 	toktype = parse_number (pstate, tokstart,
 				p - tokstart, got_dot | got_e, &yylval);
 	if (toktype == ERROR)
-	  {
-	    char *err_copy = (char *) alloca (p - tokstart + 1);
-
-	    memcpy (err_copy, tokstart, p - tokstart);
-	    err_copy[p - tokstart] = 0;
-	    error (_("Invalid number \"%s\"."), err_copy);
-	  }
+	  error (_("Invalid number \"%.*s\"."), (int) (p - tokstart),
+		 tokstart);
 	pstate->lexptr = p;
 	return toktype;
       }
-- 
2.44.0


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

* Re: [PATCH] Remove some alloca uses
  2024-04-17 22:52 [PATCH] Remove some alloca uses Tom Tromey
@ 2024-04-20 20:27 ` John Baldwin
  2024-04-22  9:41 ` [PUSHED] gdb: fix unknown variable typo in c-exp.y Andrew Burgess
  1 sibling, 0 replies; 3+ messages in thread
From: John Baldwin @ 2024-04-20 20:27 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 4/17/24 3:52 PM, Tom Tromey wrote:
> A few spots (mostly in the parsers) use alloca to ensure that a string
> is terminated before passing it to a printf-like function (mostly
> 'error').  However, this isn't needed as the "%.*s" format can be used
> instead.
> 
> This patch makes this change.
> 
> In one spot the alloca is dead code and is simply removed.

Approved-By: John Baldwin <jhb@FreeBSD.org>

-- 
John Baldwin


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

* [PUSHED] gdb: fix unknown variable typo in c-exp.y
  2024-04-17 22:52 [PATCH] Remove some alloca uses Tom Tromey
  2024-04-20 20:27 ` John Baldwin
@ 2024-04-22  9:41 ` Andrew Burgess
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Burgess @ 2024-04-22  9:41 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

Fix 'val' -> 'value' typo in c-exp.y which was breaking the build.
Introduced in commit:

  commit e6375bc8ebbbc177c79f08e9616eb0b131229f65
  Date:   Wed Apr 17 16:17:33 2024 -0600

      Remove some alloca uses
---
 gdb/c-exp.y | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index 87aca4d59b1..5db63551d21 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -3430,7 +3430,7 @@ c_print_token (FILE *file, int type, YYSTYPE value)
     case CHAR:
     case STRING:
       parser_fprintf (file, "tsval<type=%d, %.*s>", value.tsval.type,
-		      value.tsval.length, val.tsval.ptr);
+		      value.tsval.length, value.tsval.ptr);
       break;
 
     case NSSTRING:

base-commit: 911cb006bf029ada9f6364619257dffe9218e92a
-- 
2.25.4


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

end of thread, other threads:[~2024-04-22  9:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-17 22:52 [PATCH] Remove some alloca uses Tom Tromey
2024-04-20 20:27 ` John Baldwin
2024-04-22  9:41 ` [PUSHED] gdb: fix unknown variable typo in c-exp.y Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).