From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 10/13] gdb: remove some uses of alloca from printcmd.c
Date: Mon, 27 Feb 2023 21:29:23 +0000 [thread overview]
Message-ID: <6329d414746b1b6fe6d5c32d9ab5c183b5719719.1677533215.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1677533215.git.aburgess@redhat.com>
Remove a couple of uses of alloca from printcmd.c, and replace them
with gdb::byte_vector.
Unlike most times when alloca is replace with gdb::byte_vector, the
size of the vector is not specified at the byte_vector creation time.
In this case the vector can be sized differently depending on which
path of an `if` is taken, however, the lifetime of the data within the
vector must extend beyond the `if` itself.
There is a remaining use of alloca in this file, but that case will
not be replace with gdb::byte_vector, so I've left that for now.
There should be no user visible changes after this commit.
---
gdb/printcmd.c | 44 +++++++++++++++++++++-----------------------
1 file changed, 21 insertions(+), 23 deletions(-)
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 0d3bc292d4e..1efd3cabf54 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -2443,7 +2443,7 @@ static void
printf_c_string (struct ui_file *stream, const char *format,
struct value *value)
{
- const gdb_byte *str;
+ gdb::byte_vector str;
if (value->type ()->code () != TYPE_CODE_PTR
&& value->lval () == lval_internalvar
@@ -2455,11 +2455,10 @@ printf_c_string (struct ui_file *stream, const char *format,
character. This protects against corrupted C-style strings that lack
the terminating null char. It also allows Ada-style strings (not
null terminated) to be printed without problems. */
- gdb_byte *tem_str = (gdb_byte *) alloca (len + 1);
+ str.resize (len + 1);
- memcpy (tem_str, value->contents ().data (), len);
- tem_str [len] = 0;
- str = tem_str;
+ memcpy (str.data (), value->contents ().data (), len);
+ str [len] = 0;
}
else
{
@@ -2474,31 +2473,30 @@ printf_c_string (struct ui_file *stream, const char *format,
return;
}
- /* This is a %s argument. Find the length of the string. */
- size_t len;
-
- for (len = 0;; len++)
+ /* This is a %s argument. Build the string in STR which is
+ currently empty. */
+ gdb_assert (str.size () == 0);
+ for (size_t len = 0;; len++)
{
gdb_byte c;
QUIT;
read_memory (tem + len, &c, 1);
+ str.push_back (c);
if (c == 0)
break;
}
- /* Copy the string contents into a string inside GDB. */
- gdb_byte *tem_str = (gdb_byte *) alloca (len + 1);
-
- if (len != 0)
- read_memory (tem, tem_str, len);
- tem_str[len] = 0;
- str = tem_str;
+ /* We will have passed through the above loop at least once, and will
+ only exit the loop when we have pushed a zero byte onto the end of
+ STR. */
+ gdb_assert (str.size () > 0);
+ gdb_assert (str.back () == 0);
}
DIAGNOSTIC_PUSH
DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
- gdb_printf (stream, format, (char *) str);
+ gdb_printf (stream, format, (char *) str.data ());
DIAGNOSTIC_POP
}
@@ -2539,23 +2537,23 @@ printf_wide_c_string (struct ui_file *stream, const char *format,
/* This is a %s argument. Find the length of the string. */
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
- gdb_byte *buf = (gdb_byte *) alloca (wcwidth);
+ gdb::byte_vector buf (wcwidth);
for (len = 0;; len += wcwidth)
{
QUIT;
- read_memory (tem + len, buf, wcwidth);
- if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
+ read_memory (tem + len, buf.data (), wcwidth);
+ if (extract_unsigned_integer (buf.data (), wcwidth, byte_order) == 0)
break;
}
/* Copy the string contents into a string inside GDB. */
- gdb_byte *tem_str = (gdb_byte *) alloca (len + wcwidth);
+ gdb::byte_vector tem_str (len + wcwidth);
if (len != 0)
- read_memory (tem, tem_str, len);
+ read_memory (tem, tem_str.data (), len);
memset (&tem_str[len], 0, wcwidth);
- str = tem_str;
+ str = tem_str.data ();
}
auto_obstack output;
--
2.25.4
next prev parent reply other threads:[~2023-02-27 21:29 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-27 21:29 [PATCH 00/13] Remove a bunch of alloca uses Andrew Burgess
2023-02-27 21:29 ` [PATCH 01/13] gdb: remove uses of alloca from arch-utils.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 02/13] gdb: remove use of alloca from auxv.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 03/13] gdb: remove use of alloca from c-lang.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 04/13] gdb: remove use of alloca from corefile.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 05/13] gdb: remove uses of alloca from dwarf2/expr.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 06/13] gdb: remove a use of alloca from elfread.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 07/13] gdb: remove use of alloca from findvar.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 08/13] gdb: remove use of alloca from linux-nat-trad.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 09/13] gdb: remove use of alloca from mem-break.c Andrew Burgess
2023-02-27 21:29 ` Andrew Burgess [this message]
2023-02-27 21:29 ` [PATCH 11/13] gdb: remove some uses of alloca from remote.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 12/13] gdb: remove uses of alloca from valprint.c Andrew Burgess
2023-02-27 21:29 ` [PATCH 13/13] gdb: remove a use of alloca from symfile.c Andrew Burgess
2023-02-28 2:47 ` [PATCH 00/13] Remove a bunch of alloca uses Simon Marchi
2023-03-07 21:57 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6329d414746b1b6fe6d5c32d9ab5c183b5719719.1677533215.git.aburgess@redhat.com \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).