From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 5/5] gdbserver: use unique_xmalloc_ptr to avoid memory leaks
Date: Mon, 16 Nov 2026 11:10:11 +0000 [thread overview]
Message-ID: <4ec1b0906acab6dd698aa3a3685a779f8b41c93f.1794827178.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1794827178.git.aburgess@redhat.com>
In tracepoint.cc there are a few memory buffers allocated with malloc
which are live during a call to hex2bin. The hex2bin function can
throw an exception which will cause the memory buffers to leak.
Prevent this by holding the memory buffers in
gdb::unique_xmalloc_ptr<char> variables, so that they will be
deallocated if an exception is thrown. These changes are in
cmd_qtdpsrc, cmd_qtdv, and cmd_qtnotes.
Currently these buffers are placed into a data structure and managed
as a raw pointer, so I am still releasing the pointers from the
gdb::unique_xmalloc_ptr<char>. In the future it might be an
improvement to hold the pointers long term as
gdb::unique_xmalloc_ptr<char>, but I'm leaving that change to another
day.
There should be no significant user visible changes after this commit,
other than fewer memory leaks.
I've extended gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
with a non-hex test case in the test_qtdv proc, and a new
test_qtnotes proc; these exercise the invalid packet error paths.
Currently, for the tracepoint packets, the hex2bin exception is only
caught in captured_main (gdbserver/server.cc) where either gdbserver
will exit, or the connection will be reset to its default state
(depending on whether the --once flag was passed, see notes below).
This feels a little harsh for an invalid packet. We should just
return an error and ignore the packet, but I don't plan to change this
behaviour in this commit. As a result some of the new tests, those
that pass non-hex characters, expect gdbserver to exit after the
invalid packet. The memory leaks are visible when running under
valgrind or with the address sanitizers built in.
A note on how the --once flag (or lack thereof) affects the connection
when an exception is caught. I noticed during testing that if an
exception is caught in captured_main, and the --once flag was not
used, then gdbserver will return to the top of its connection loop,
which resets the connection state. However, gdbserver doesn't drop
any active connection. This leaves the connection in a weird state;
GDB is unaware that the per-connection settings have been reset, so
for things like noack_mode, GDB might think the flag is set to true,
while gdbserver has actually reset the flag to false. This is a
separate issue that I plan to address in a follow-up commit, not as
part of this series, which focuses on memory issues.
---
.../gdb.server/invalid-tracepoint-packets.exp | 11 ++-
gdbserver/tracepoint.cc | 70 ++++++++-----------
2 files changed, 40 insertions(+), 41 deletions(-)
diff --git a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
index cbaddfd7e7b..b5f2a67b9ef 100644
--- a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
+++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
@@ -162,7 +162,10 @@ proc_with_prefix test_qtdv {} {
gdb_test "maint packet QTinit" $::ok_re \
"valid QTinit packet accepted"
- gdbserver_exit false
+ # Non-hex characters in the variable string name.
+ gdb_test "maint packet QTDV:3:5:0:zz" $::error_re "QTDV name is non-hex"
+
+ ensure_gdbserver_exited_due_to_non_hex
}
# Test the QTDPsrc packet.
@@ -433,7 +436,11 @@ proc_with_prefix test_qtnotes {} {
"received: \"\[^\r\n\]*username:484950;notes:515253\[^\r\n\]*\"" \
"check notes with qTStatus packet"
- gdbserver_exit false
+ # Non-hex characters in the packet.
+ gdb_test "maint packet QTNotes:user:zz;" $::error_re \
+ "QTNotes user field is non-hex"
+
+ ensure_gdbserver_exited_due_to_non_hex
}
test_qtdv
diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index 1e68b19d9d5..f1f9a1d6859 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -2585,9 +2585,8 @@ cmd_qtdpsrc (char *own_buf)
write_enn (own_buf);
return;
}
- char *srctype = (char *) xmalloc (packet - start_of_type + 1);
- memcpy (srctype, start_of_type, packet - start_of_type);
- srctype[packet - start_of_type] = '\0';
+ gdb::unique_xmalloc_ptr<char> srctype
+ = make_unique_xstrndup (start_of_type, packet - start_of_type);
++packet;
/* The QTDPsrc spec says that SLEN can be longer than the delivered string
@@ -2601,16 +2600,10 @@ cmd_qtdpsrc (char *own_buf)
ULONGEST start, slen;
packet = unpack_varlen_hex (packet, &start);
if (!check_char_and_skip_or_write_error (own_buf, &packet, ':'))
- {
- xfree (srctype);
- return;
- }
+ return;
packet = unpack_varlen_hex (packet, &slen);
if (!check_char_and_skip_or_write_error (own_buf, &packet, ':'))
- {
- xfree (srctype);
- return;
- }
+ return;
size_t len = strlen (packet);
if (slen != len / 2 || len % 2 != 0)
@@ -2618,14 +2611,13 @@ cmd_qtdpsrc (char *own_buf)
trace_debug ("Tracepoint error: invalid string length %s in: %s",
pulongest (slen), own_buf);
write_enn (own_buf);
- xfree (srctype);
return;
}
- char *src = (char *) xmalloc (len / 2 + 1);
- size_t nbytes = hex2bin (packet, (gdb_byte *) src,
+ gdb::unique_xmalloc_ptr<char> src ((char *) xmalloc (len / 2 + 1));
+ size_t nbytes = hex2bin (packet, (gdb_byte *) src.get (),
static_cast<int> (len / 2));
- src[nbytes] = '\0';
+ src.get ()[nbytes] = '\0';
/* Because we check SLEN against the packet length above, either the packet
is fully parsed, or hex2bin will have thrown an error on seeing a
@@ -2633,8 +2625,8 @@ cmd_qtdpsrc (char *own_buf)
gdb_assert (packet[nbytes * 2] == '\0');
struct source_string *newlast = XNEW (struct source_string);
- newlast->type = srctype;
- newlast->str = src;
+ newlast->type = srctype.release ();
+ newlast->str = src.release ();
newlast->next = nullptr;
/* Always add a source string to the end of the list;
this keeps sequences of actions/commands in the right
@@ -2673,9 +2665,9 @@ cmd_qtdv (char *own_buf)
return;
size_t nbytes = strlen (packet) / 2;
- char *varname = (char *) xmalloc (nbytes + 1);
- nbytes = hex2bin (packet, (gdb_byte *) varname, nbytes);
- varname[nbytes] = '\0';
+ gdb::unique_xmalloc_ptr<char> varname ((char *) xmalloc (nbytes + 1));
+ nbytes = hex2bin (packet, (gdb_byte *) varname.get (), nbytes);
+ varname.get ()[nbytes] = '\0';
struct trace_state_variable *tsv = create_trace_state_variable (num, 1);
@@ -2687,12 +2679,11 @@ cmd_qtdv (char *own_buf)
it. */
if (tsv->getter != nullptr)
{
- if (builtin != 0 && strcmp (varname, tsv->name) == 0)
+ if (builtin != 0 && strcmp (varname.get (), tsv->name) == 0)
{
/* The client is re-sending the definition of this builtin trace
state variable unchanged. There's nothing to do. */
write_ok (own_buf);
- xfree (varname);
return;
}
@@ -2700,7 +2691,6 @@ cmd_qtdv (char *own_buf)
getter function is not being updated this will not do what the
client expects, so reject it. */
write_enn (own_buf);
- xfree (varname);
return;
}
@@ -2710,7 +2700,7 @@ cmd_qtdv (char *own_buf)
xfree (tsv->name);
tsv->initial_value = (LONGEST) val;
- tsv->name = varname;
+ tsv->name = varname.release ();
set_trace_state_variable_value (num, (LONGEST) val);
@@ -3953,13 +3943,13 @@ cmd_qtnotes (char *own_buf)
return;
}
size_t nbytes = (packet - start_of_text) / 2;
- char *user = (char *) xmalloc (nbytes + 1);
- nbytes = hex2bin (start_of_text, (gdb_byte *) user, nbytes);
- user[nbytes] = '\0';
+ gdb::unique_xmalloc_ptr<char> user ((char *) xmalloc (nbytes + 1));
+ nbytes = hex2bin (start_of_text, (gdb_byte *) user.get (), nbytes);
+ user.get ()[nbytes] = '\0';
++packet; /* skip the semicolon */
- trace_debug ("User is '%s'", user);
+ trace_debug ("User is '%s'", user.get ());
xfree (tracing_user_name);
- tracing_user_name = user;
+ tracing_user_name = user.release ();
}
else if (startswith (packet, "notes:"))
{
@@ -3973,13 +3963,13 @@ cmd_qtnotes (char *own_buf)
return;
}
size_t nbytes = (packet - start_of_text) / 2;
- char *notes = (char *) xmalloc (nbytes + 1);
- nbytes = hex2bin (start_of_text, (gdb_byte *) notes, nbytes);
- notes[nbytes] = '\0';
+ gdb::unique_xmalloc_ptr<char> notes ((char *) xmalloc (nbytes + 1));
+ nbytes = hex2bin (start_of_text, (gdb_byte *) notes.get (), nbytes);
+ notes.get ()[nbytes] = '\0';
++packet; /* skip the semicolon */
- trace_debug ("Notes is '%s'", notes);
+ trace_debug ("Notes is '%s'", notes.get ());
xfree (tracing_notes);
- tracing_notes = notes;
+ tracing_notes = notes.release ();
}
else if (startswith (packet, "tstop:"))
{
@@ -3993,13 +3983,15 @@ cmd_qtnotes (char *own_buf)
return;
}
size_t nbytes = (packet - start_of_text) / 2;
- char *stopnote = (char *) xmalloc (nbytes + 1);
- nbytes = hex2bin (start_of_text, (gdb_byte *) stopnote, nbytes);
- stopnote[nbytes] = '\0';
+ gdb::unique_xmalloc_ptr<char> stopnote
+ ((char *) xmalloc (nbytes + 1));
+ nbytes = hex2bin (start_of_text, (gdb_byte *) stopnote.get (),
+ nbytes);
+ stopnote.get ()[nbytes] = '\0';
++packet; /* skip the semicolon */
- trace_debug ("tstop note is '%s'", stopnote);
+ trace_debug ("tstop note is '%s'", stopnote.get ());
xfree (tracing_stop_note);
- tracing_stop_note = stopnote;
+ tracing_stop_note = stopnote.release ();
}
else
break;
--
2.25.4
next prev parent reply other threads:[~2026-11-16 11:12 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 11:10 [PATCH 0/5] gdbserver: fix buffer overflow issue in QTDPsrc packet Andrew Burgess
2026-11-16 11:10 ` [PATCH 1/5] gdbserver: clean up some older code in tracepoint.cc Andrew Burgess
2026-11-16 11:10 ` [PATCH 2/5] gdbserver: prevent buffer overrun for QTDPsrc packet Andrew Burgess
2026-11-19 19:51 ` Keith Seitz
2026-11-23 17:15 ` Andrew Burgess
2026-11-23 17:57 ` Keith Seitz
2026-11-16 11:10 ` [PATCH 3/5] gdbserver: improve packet parsing in tracepoint.cc Andrew Burgess
2026-11-16 11:10 ` [PATCH 4/5] gdbserver: don't leak trace variable names if reusing variable numbers Andrew Burgess
2026-11-16 11:10 ` Andrew Burgess [this message]
2026-11-19 19:52 ` [PATCH 0/5] gdbserver: fix buffer overflow issue in QTDPsrc packet Keith Seitz
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=4ec1b0906acab6dd698aa3a3685a779f8b41c93f.1794827178.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