]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Remove some alloca uses
authorTom Tromey <tom@tromey.com>
Wed, 17 Apr 2024 22:17:33 +0000 (16:17 -0600)
committerTom Tromey <tom@tromey.com>
Sun, 21 Apr 2024 18:12:43 +0000 (12:12 -0600)
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>
gdb/c-exp.y
gdb/cp-name-parser.y
gdb/cp-support.c
gdb/d-exp.y
gdb/f-exp.y
gdb/go-exp.y
gdb/m2-exp.y
gdb/p-exp.y

index 663c30f9517a5cf967edbe8756c697aa626622d3..87aca4d59b1e984a169a9acc219d160437d8a2cd 100644 (file)
@@ -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:
index 87f13445bbaf4cc15ad990bc8c322b155cb70d79..e944276d00184df124474e6af30e6de4414d32a2 100644 (file)
@@ -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;
          }
index e6e811ddf503d40f7f40edebb6c103bb2ff42fc3..5fd53094d053cca9fdf7aff06e29f8cb461ffb8c 100644 (file)
@@ -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".  */
index b2adad24d1a924464f4bbf255397f28ca943dad5..13d2cfa44d8817d1cd8a15aa445d95abc760d459 100644 (file)
@@ -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;
       }
index 11cd7948682ef1bbefe02ab8aac2a7dcf487c304..bdf9c32a81b360872ef4294f5d922b98b2980c57 100644 (file)
@@ -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;
       }
index 20ab8ff76cf8ea9652350784c1c0c7933d7f07c3..1a6ebbe135bebe744124e2043cad4919f9425f45 100644 (file)
@@ -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;
       }
index ebbc49c62a5e82df2139be8388091fc55f1aad0e..28005e1a70008bc6331405f3af4ac7082a099f22 100644 (file)
@@ -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;
     }
index 2140b609225169c8cd4c4cf1f23c03b6f54e7efa..f334db6b5237a772811ac6860c8691d46685de58 100644 (file)
@@ -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;
       }