]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdbsupport: remove remaining alloca uses
authorSimon Marchi <simon.marchi@polymtl.ca>
Mon, 15 Sep 2025 14:40:30 +0000 (10:40 -0400)
committerSimon Marchi <simon.marchi@polymtl.ca>
Mon, 15 Sep 2025 14:40:52 +0000 (10:40 -0400)
Remove the three remaining uses of alloca in gdbsupport.

I only built-tested the Windows-only portion in pathstuff.cc.

Change-Id: Ie588fa57f43de900d5f42e93a8875a7da462404b
Reviewed-By: Keith Seitz <keiths@redhat.com>
gdbsupport/filestuff.cc
gdbsupport/format.cc
gdbsupport/pathstuff.cc

index 5c1817e35297607e54c387d8c08a8f9c03287ed6..817663bae6a97147c094df5549ea996e0f8f0a32 100644 (file)
@@ -333,14 +333,10 @@ gdb_fopen_cloexec (const char *filename, const char *opentype)
 
   if (!fopen_e_ever_failed_einval)
     {
-      char *copy;
-
-      copy = (char *) alloca (strlen (opentype) + 2);
-      strcpy (copy, opentype);
       /* This is a glibc extension but we try it unconditionally on
         this path.  */
-      strcat (copy, "e");
-      result = fopen (filename, copy);
+      auto opentype_e = std::string (opentype) + 'e';
+      result = fopen (filename, opentype_e.c_str ());
 
       if (result == NULL && errno == EINVAL)
        {
index 00fdc8d654897363a9aba609368b913a54541197..9d538e983accd3f485fa63053bf525183841c3f8 100644 (file)
@@ -25,6 +25,9 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
   const char *s = *arg;
   const char *string;
 
+  /* Buffer to hold the escaped-processed version of the string.  */
+  std::string de_escaped;
+
   if (gdb_extensions)
     {
       string = *arg;
@@ -34,10 +37,6 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
     {
       /* Parse the format-control string and copy it into the string STRING,
         processing some kinds of escape sequence.  */
-
-      char *f = (char *) alloca (strlen (s) + 1);
-      string = f;
-
       while (*s != '"' && *s != '\0')
        {
          int c = *s++;
@@ -50,34 +49,34 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
              switch (c = *s++)
                {
                case '\\':
-                 *f++ = '\\';
+                 de_escaped += '\\';
                  break;
                case 'a':
-                 *f++ = '\a';
+                 de_escaped += '\a';
                  break;
                case 'b':
-                 *f++ = '\b';
+                 de_escaped += '\b';
                  break;
                case 'e':
-                 *f++ = '\e';
+                 de_escaped += '\e';
                  break;
                case 'f':
-                 *f++ = '\f';
+                 de_escaped += '\f';
                  break;
                case 'n':
-                 *f++ = '\n';
+                 de_escaped += '\n';
                  break;
                case 'r':
-                 *f++ = '\r';
+                 de_escaped += '\r';
                  break;
                case 't':
-                 *f++ = '\t';
+                 de_escaped += '\t';
                  break;
                case 'v':
-                 *f++ = '\v';
+                 de_escaped += '\v';
                  break;
                case '"':
-                 *f++ = '"';
+                 de_escaped += '"';
                  break;
                default:
                  /* ??? TODO: handle other escape sequences.  */
@@ -87,12 +86,11 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
              break;
 
            default:
-             *f++ = c;
+             de_escaped += c;
            }
        }
 
-      /* Terminate our escape-processed copy.  */
-      *f++ = '\0';
+      string = de_escaped.c_str ();
 
       /* Whether the format string ended with double-quote or zero, we're
         done with it; it's up to callers to complain about syntax.  */
index 1866949d62daf34ad3745b617ff40b69667cc9c2..8142bd52a65f7898efbfff5ceef988c360f56b7d 100644 (file)
@@ -89,34 +89,25 @@ std::string
 gdb_realpath_keepfile (const char *filename)
 {
   const char *base_name = lbasename (filename);
-  char *dir_name;
 
   /* Extract the basename of filename, and return immediately
      a copy of filename if it does not contain any directory prefix.  */
   if (base_name == filename)
     return filename;
 
-  dir_name = (char *) alloca ((size_t) (base_name - filename + 2));
-  /* Allocate enough space to store the dir_name + plus one extra
-     character sometimes needed under Windows (see below), and
-     then the closing \000 character.  */
-  strncpy (dir_name, filename, base_name - filename);
-  dir_name[base_name - filename] = '\000';
+  std::string dir_name (filename, base_name - filename);
 
 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
   /* We need to be careful when filename is of the form 'd:foo', which
      is equivalent of d:./foo, which is totally different from d:/foo.  */
-  if (strlen (dir_name) == 2 && c_isalpha (dir_name[0]) && dir_name[1] == ':')
-    {
-      dir_name[2] = '.';
-      dir_name[3] = '\000';
-    }
+  if (dir_name.size () == 2 && c_isalpha (dir_name[0]) && dir_name[1] == ':')
+    dir_name += '.';
 #endif
 
   /* Canonicalize the directory prefix, and build the resulting
      filename.  If the dirname realpath already contains an ending
      directory separator, avoid doubling it.  */
-  gdb::unique_xmalloc_ptr<char> path_storage = gdb_realpath (dir_name);
+  gdb::unique_xmalloc_ptr<char> path_storage = gdb_realpath (dir_name.c_str ());
   const char *real_path = path_storage.get ();
   return path_join (real_path, base_name);
 }