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)
{
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)
{

View 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. */

View 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);
}