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 <keiths@redhat.com>
This commit is contained in:
Simon Marchi
2025-09-15 10:40:30 -04:00
parent 67a611d658
commit 51b281ccfa
3 changed files with 21 additions and 36 deletions

View File

@@ -333,14 +333,10 @@ gdb_fopen_cloexec (const char *filename, const char *opentype)
if (!fopen_e_ever_failed_einval) 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 is a glibc extension but we try it unconditionally on
this path. */ this path. */
strcat (copy, "e"); auto opentype_e = std::string (opentype) + 'e';
result = fopen (filename, copy); result = fopen (filename, opentype_e.c_str ());
if (result == NULL && errno == EINVAL) if (result == NULL && errno == EINVAL)
{ {

View File

@@ -25,6 +25,9 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
const char *s = *arg; const char *s = *arg;
const char *string; const char *string;
/* Buffer to hold the escaped-processed version of the string. */
std::string de_escaped;
if (gdb_extensions) if (gdb_extensions)
{ {
string = *arg; 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, /* Parse the format-control string and copy it into the string STRING,
processing some kinds of escape sequence. */ processing some kinds of escape sequence. */
char *f = (char *) alloca (strlen (s) + 1);
string = f;
while (*s != '"' && *s != '\0') while (*s != '"' && *s != '\0')
{ {
int c = *s++; int c = *s++;
@@ -50,34 +49,34 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
switch (c = *s++) switch (c = *s++)
{ {
case '\\': case '\\':
*f++ = '\\'; de_escaped += '\\';
break; break;
case 'a': case 'a':
*f++ = '\a'; de_escaped += '\a';
break; break;
case 'b': case 'b':
*f++ = '\b'; de_escaped += '\b';
break; break;
case 'e': case 'e':
*f++ = '\e'; de_escaped += '\e';
break; break;
case 'f': case 'f':
*f++ = '\f'; de_escaped += '\f';
break; break;
case 'n': case 'n':
*f++ = '\n'; de_escaped += '\n';
break; break;
case 'r': case 'r':
*f++ = '\r'; de_escaped += '\r';
break; break;
case 't': case 't':
*f++ = '\t'; de_escaped += '\t';
break; break;
case 'v': case 'v':
*f++ = '\v'; de_escaped += '\v';
break; break;
case '"': case '"':
*f++ = '"'; de_escaped += '"';
break; break;
default: default:
/* ??? TODO: handle other escape sequences. */ /* ??? TODO: handle other escape sequences. */
@@ -87,12 +86,11 @@ format_pieces::format_pieces (const char **arg, bool gdb_extensions,
break; break;
default: default:
*f++ = c; de_escaped += c;
} }
} }
/* Terminate our escape-processed copy. */ string = de_escaped.c_str ();
*f++ = '\0';
/* Whether the format string ended with double-quote or zero, we're /* Whether the format string ended with double-quote or zero, we're
done with it; it's up to callers to complain about syntax. */ done with it; it's up to callers to complain about syntax. */

View File

@@ -89,34 +89,25 @@ std::string
gdb_realpath_keepfile (const char *filename) gdb_realpath_keepfile (const char *filename)
{ {
const char *base_name = lbasename (filename); const char *base_name = lbasename (filename);
char *dir_name;
/* Extract the basename of filename, and return immediately /* Extract the basename of filename, and return immediately
a copy of filename if it does not contain any directory prefix. */ a copy of filename if it does not contain any directory prefix. */
if (base_name == filename) if (base_name == filename)
return filename; return filename;
dir_name = (char *) alloca ((size_t) (base_name - filename + 2)); std::string dir_name (filename, base_name - filename);
/* 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';
#ifdef HAVE_DOS_BASED_FILE_SYSTEM #ifdef HAVE_DOS_BASED_FILE_SYSTEM
/* We need to be careful when filename is of the form 'd:foo', which /* 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. */ 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] == ':') if (dir_name.size () == 2 && c_isalpha (dir_name[0]) && dir_name[1] == ':')
{ dir_name += '.';
dir_name[2] = '.';
dir_name[3] = '\000';
}
#endif #endif
/* Canonicalize the directory prefix, and build the resulting /* Canonicalize the directory prefix, and build the resulting
filename. If the dirname realpath already contains an ending filename. If the dirname realpath already contains an ending
directory separator, avoid doubling it. */ 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 (); const char *real_path = path_storage.get ();
return path_join (real_path, base_name); return path_join (real_path, base_name);
} }