From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id qdYYGCHl+mp4/xIAWB0awg (envelope-from ) for ; Mon, 16 Nov 2026 06:12:33 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hHtNzabw; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 5E4ED1E091; Mon, 16 Nov 2026 06:12:33 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 9A95C1E043 for ; Mon, 16 Nov 2026 06:12:32 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 10A5F4BA9010 for ; Mon, 16 Nov 2026 11:12:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 10A5F4BA9010 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hHtNzabw Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 676F44B920C2 for ; Mon, 16 Nov 2026 11:10:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 676F44B920C2 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 676F44B920C2 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794827436; cv=none; b=Y9VxRK4ypaCau98dhckWeEvBb76j7OhQePtd7R/zqVWMIauA8X4AR6scoyiNFY6GCUKHl+3royq6a5VJxk/iQPrJk6i+/W226TXKitVOD89E3j3vZUuZD0z+sKbsE/orKMoxvQSGSBKAEWl3/e5dapchrssYs3kmw4YWRkCicyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794827436; c=relaxed/simple; bh=5TL6pH/LMI9zS0GtQORrsjHmfFuTUTiqF0bNKjZV+k8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=dg15+EvHZ8QgGgCom7vMho017Tzi19AM1IUCllJc5LgPXF9fG5jkrutQMlmJ/kKdApWl2UtuMCYgcijNDimuW5xM/UWNbQbyvBsi/KpsZPhfCx2En6PEooKZbRdKRJPorSFR9YaaWIzcP5IIFfen/4WiAafTY5QlZe2Hho668j0= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hHtNzabw DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 676F44B920C2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794827434; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rtXBNaIsQ96Af3gWB1L6pYu+1fnOqACB3S1dTinpYas=; b=hHtNzabwSAnsUQjsCp0+rM7UVj1fiT7L9T7zZtXdqkXWdSubbetPQuRUqpa4CxDbqrshbO ux7cU+jx7RAl+K/TJ/2GArZ2z6pN7A86M+5r7HQo/4w/NUGQ5VEvmV0Jd/HAuMKGaqh8dI S3+dqpfMm8Gn7E/zaTJFA6wTDUo+gLg= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-389-HKpAqkH9O6-0tnJfYOGR3A-1; Mon, 16 Nov 2026 06:10:26 -0500 X-MC-Unique: HKpAqkH9O6-0tnJfYOGR3A-1 X-Mimecast-MFC-AGG-ID: HKpAqkH9O6-0tnJfYOGR3A_1794827423 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-48e011829easo1558773f8f.3 for ; Mon, 16 Nov 2026 03:10:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794827423; x=1795432223; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=rtXBNaIsQ96Af3gWB1L6pYu+1fnOqACB3S1dTinpYas=; b=U6cIm6HW6gENY3Zg4vL9Z6pswD1VjgTO1Cravg6Ey5ghSs4iFNx+FKwj9HWhTCc8OH tczuNbAcXet2vkqk4arHxxZi5X9WKjHjC3WzpD8CTQiafXEAwkaxpHQxDCxg1t5UylXC /BTTOh39oCKo9feauCSQq4iOLqw8EGD+OT9+dLlWE6Rb3Uzp6hv00iHfUsD7k/I3UvnU JX71CmR2gZiUD6kz9sryNj64didKy4Xat6fmGD3FLrkCI9o4IQo5xDXu3SQVfT5vTsWc sB7rDEHPfdXmm9LkrvMvCCRyj4frLfwHwAnOg8p6RIceh+jkl16WAnyjIbwnfuqkqp8Z niZg== X-Gm-Message-State: APuU4Z9XQM6nZlHZBuew7ZiGsReuqrfbGYdsB2tyfef5zI2TOOPsWQrE xhiDgTbnM7P0ABLX1SxjlrijNnwkBYz1DPQ050EEjMn2sjy6J8v85mKPjFmYZ7u5CMBDQyqurId 8LzIFtopt+V+I+QkVNCvWFQyv1dqUqRnm7ESOrbN4cBCBvUUKda3b++BdsG6C6W2zj8mog4xahF /a4/WhBGNS6cT1U8eGRf5X1E9EBvJAR0tGvnf4qilMrgC8OAk= X-Gm-Gg: AZFdTZfVa4jTa+WFSESTZEgKOkoXRJIslQgXeVGhhnUAN04aVgbkAwTJM74Rou14r93 szWY7F02myKvgEiVBGDTlxBRXGjl70tcuQvvdEw7eA2V8LwbpDK+xiNooKAIWfgGva5O+q8MOFH 44YLzbQSeb8wyAEZiFLOVLyKwglQ4OawGtgHyms2+TMHN/8HIoFqQf26gb6zRNF1GqLfwqybw/q xWjiQtkneVhaPlN2O+BNkeouGB2l8piIGdhPvs17rc5czLL8yvLs1d3+2Y1fAVrg8dryonYWZvl B5TWud2iL53FYZG9495FYLdX8Ld3dGyEhQ45Ehd7Qz/umTccGsQxV3YmWWx/gqkC0q/QIzmLmoN QDKtt5PvPMSFrJi0Xm+Iggt4k1/lrQfEVEY8IaOdedxd3 X-Received: by 2002:a5d:6f07:0:b0:490:f98f:addc with SMTP id ffacd0b85a97d-49c71d48742mr21036819f8f.27.1794827422804; Mon, 16 Nov 2026 03:10:22 -0800 (PST) X-Received: by 2002:a5d:6f07:0:b0:490:f98f:addc with SMTP id ffacd0b85a97d-49c71d48742mr21036742f8f.27.1794827422115; Mon, 16 Nov 2026 03:10:22 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-49d69c21a17sm16659780f8f.50.2026.11.16.03.10.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2026 03:10:21 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 5/5] gdbserver: use unique_xmalloc_ptr to avoid memory leaks Date: Mon, 16 Nov 2026 11:10:11 +0000 Message-Id: <4ec1b0906acab6dd698aa3a3685a779f8b41c93f.1794827178.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: gauj6-asB3ZR4bmMSq2NmRmhOmdiMMIMHCMrZR_cqJA_1794827423 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 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. In the future it might be an improvement to hold the pointers long term as gdb::unique_xmalloc_ptr, 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 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 src ((char *) xmalloc (len / 2 + 1)); + size_t nbytes = hex2bin (packet, (gdb_byte *) src.get (), static_cast (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 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 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 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 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