From: Simon Marchi Date: Mon, 15 Sep 2025 14:40:30 +0000 (-0400) Subject: gdbsupport: remove remaining alloca uses X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=51b281ccfa04f22a99050629eef614d520ae602d;p=thirdparty%2Fbinutils-gdb.git gdbsupport: remove remaining alloca uses 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 --- diff --git a/gdbsupport/filestuff.cc b/gdbsupport/filestuff.cc index 5c1817e3529..817663bae6a 100644 --- a/gdbsupport/filestuff.cc +++ b/gdbsupport/filestuff.cc @@ -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) { diff --git a/gdbsupport/format.cc b/gdbsupport/format.cc index 00fdc8d6548..9d538e983ac 100644 --- a/gdbsupport/format.cc +++ b/gdbsupport/format.cc @@ -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. */ diff --git a/gdbsupport/pathstuff.cc b/gdbsupport/pathstuff.cc index 1866949d62d..8142bd52a65 100644 --- a/gdbsupport/pathstuff.cc +++ b/gdbsupport/pathstuff.cc @@ -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 path_storage = gdb_realpath (dir_name); + gdb::unique_xmalloc_ptr path_storage = gdb_realpath (dir_name.c_str ()); const char *real_path = path_storage.get (); return path_join (real_path, base_name); }