[binutils-gdb] gdbserver: use unique_xmalloc_ptr to avoid memory leaks
Andrew Burgess
aburgess@sourceware.org
Tue Dec 1 10:40:12 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=75eb64fc067101e65ef9f385ca5faa9e70af84dc
commit 75eb64fc067101e65ef9f385ca5faa9e70af84dc
Author: Andrew Burgess <aburgess@redhat.com>
Date: Wed Nov 11 17:34:37 2026 +0000
gdbserver: use unique_xmalloc_ptr to avoid memory leaks
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.server/invalid-tracepoint-packets.exp with new tests
in the test_qtdv and test_qtnotes procs.
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.
Reviewed-By: Keith Seitz <keiths@redhat.com>
Diff:
---
.../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 7aeac9e7465..3e4c79d7a50 100644
--- a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
+++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp
@@ -167,7 +167,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.
@@ -442,7 +445,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 e8d182e299a..e44df5176da 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -2618,9 +2618,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
@@ -2634,16 +2633,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)
@@ -2651,14 +2644,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
@@ -2666,8 +2658,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
@@ -2706,9 +2698,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);
@@ -2727,12 +2719,10 @@ cmd_qtdv (char *own_buf)
and the same name then accept it without updating anything.
Otherwise the client is trying to change the name of this builtin,
or stop this being a builtin, neither of which are allowed. */
- if (builtin != 0 && strcmp (varname, tsv->name) == 0)
+ if (builtin != 0 && strcmp (varname.get (), tsv->name) == 0)
write_ok (own_buf);
else
write_enn (own_buf);
-
- xfree (varname);
return;
}
@@ -2742,7 +2732,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);
@@ -3990,13 +3980,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:"))
{
@@ -4010,13 +4000,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:"))
{
@@ -4030,13 +4020,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;
More information about the Gdb-cvs
mailing list