aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2024-04-17 16:17:33 -0600
committerTom Tromey <tom@tromey.com>2024-04-21 12:12:43 -0600
commite6375bc8ebbbc177c79f08e9616eb0b131229f65 (patch)
treefca4ad6a0389268a24e79e5b007b65528cda648d
parent7e9ef24e4a72d8d174932c7dd6be44226328ab88 (diff)
downloadgdb-e6375bc8ebbbc177c79f08e9616eb0b131229f65.zip
gdb-e6375bc8ebbbc177c79f08e9616eb0b131229f65.tar.gz
gdb-e6375bc8ebbbc177c79f08e9616eb0b131229f65.tar.bz2
Remove some alloca uses
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. Approved-By: John Baldwin <jhb@FreeBSD.org>
-rw-r--r--gdb/c-exp.y19
-rw-r--r--gdb/cp-name-parser.y4
-rw-r--r--gdb/cp-support.c12
-rw-r--r--gdb/d-exp.y9
-rw-r--r--gdb/f-exp.y9
-rw-r--r--gdb/go-exp.y9
-rw-r--r--gdb/m2-exp.y9
-rw-r--r--gdb/p-exp.y9
8 files changed, 16 insertions, 64 deletions
diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index 663c30f..87aca4d 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 87f1344..e944276 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 e6e811d..5fd5309 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 b2adad2..13d2cfa 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 11cd794..bdf9c32 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 20ab8ff..1a6ebbe 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 ebbc49c..28005e1 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 2140b60..f334db6 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;
}