From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id gfZYCPbk+mp4/xIAWB0awg (envelope-from ) for ; Mon, 16 Nov 2026 06:11:50 -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=dqPMtg2z; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 1DDF11E043; Mon, 16 Nov 2026 06:11:50 -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 [38.145.34.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 CFC641E043 for ; Mon, 16 Nov 2026 06:11:48 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 951404BA2E2F for ; Mon, 16 Nov 2026 11:11:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 951404BA2E2F 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=dqPMtg2z Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id F33214BA23C4 for ; Mon, 16 Nov 2026 11:10:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F33214BA23C4 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 F33214BA23C4 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794827423; cv=none; b=pejd7i0/nH4rMaGwmTHtrt8l3r6UwCaWLlwjCMUzeLVUH8rbPT4nnV1bMU/MyHpMhGWjE3dBx08QsQ22JZdAC7ySo8zHTYEN8o0lbaN/60yCqJN3BdiLlJ0gEGIECqVsJL34NMOdCydGQXOb03+BLwyBpJrZhN2QJFSI4saeVpA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794827423; c=relaxed/simple; bh=wkAD4AOypD3/Zuq/WHjVN2/KkQ2VyEwve9pbZvqwbPk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=fPGuBPMyaeig0Vcin0CRttSi2f63pHlGY03XHC1fS0+kmTsKf5qQI3PmuIW3iVa5/ByQUnVbvb7M0HaGYHvC7HR6C3tXF2h+I5NSvRXpmklP0xOllWenOsjqvx+ib4IFN2od/Dt5pTCeCutEEu2sP3AqGYQ8fP7T4gwP7eSOffE= 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=dqPMtg2z DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F33214BA23C4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794827422; 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=5hIE1HN8kAO8Pdjomuhxp7UlisuYavxMPSbCvY8Z8zI=; b=dqPMtg2ziCBOru1l0KI9iJBgwtalSMxckOfRcjPHSKjKasBNKcptmaWpFlT6Wr8yJtAoc9 txZ19F5dWvpvBc64svzLDJ5Qd2HJtHmxPkjF5H6YO8aQy0UQOsAZAshlfP4L59tJbxxcyy 4N9Vwla1EKBsOoD+dtRbOXk5t2NsQAQ= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-178-9lFG5DerMEug0cIMWUw1Sg-1; Mon, 16 Nov 2026 06:10:21 -0500 X-MC-Unique: 9lFG5DerMEug0cIMWUw1Sg-1 X-Mimecast-MFC-AGG-ID: 9lFG5DerMEug0cIMWUw1Sg_1794827420 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-49953abe51fso19719275e9.1 for ; Mon, 16 Nov 2026 03:10:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794827420; x=1795432220; 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=5hIE1HN8kAO8Pdjomuhxp7UlisuYavxMPSbCvY8Z8zI=; b=AaSuCrplTVho5jfKtcifx4qtsa8s+mjqWYhKC3uGiom7tYJ5IMyLXwbZ2swW8G0VUg qu1IudaF9a7TR6occDVeHM4qI59OLYyP7Pz5p5q8sP2/hLwg/NN+XDV+njNfvQiHQ8/K LOYzqpV05vqXUuVTdwyz88Rzn3BOuW+64lAj/dUhOIisoGcvu51FT63rmJlt0O+gBSpK HaTl/6HVrRFI4h2SBRq1IUbpmF3yD1F9XmGUxrxyjNPzMwL7RoPq3YaX01RUk9GUeNDX J4Oa3zuq0423V0kq785jnZPiGWwPB4w+y+Pah2TI2oko/AlJ79upoWTUqHCExhYAA82q YzzQ== X-Gm-Message-State: APuU4Z/tYYVaGKyO/6Nh9IR/W9JiuIIZT7Y8S00P2RaXnFxT9u4wQFzy 3OMcwsueHwBfVqO6M0mTap9v9BHq03c3KxG0HPHfxHwcYhsUmy44LkU7vE2vH+XJ/0FB03MaynE S6o6hzzxR3br1jfi6csfAXJqwWoj84hVK2pFdUEHfq1lYtQbBsg+r1S10K29J9oBJqKYnBmfUDB xz59bfSh1Gj/p1EzCiLi/Yke/uHj7Aew1VRc33rVz3OumstDc= X-Gm-Gg: AYBFou1ichKunHf2xtjJCbUDekQIBj7zL+KKAljChxVgHiOiXBpLmi+K9JBYWT16EyJ 62f1+XwlwPIWhZ6QVtBiN7qypgUzCKjoVSNviiLCYTW/GXJ49QsRbRri0z4h8RYi0ZQrKDXQkdb YW/3MyntrpTxc18hC+qwfm//Thml7KF/RYEuc//pdBy8fdQQ/aor4drsRYQroLvHa/DCjRkwITj zLOYIhJCiETEL+l1oDmHu8CJvG0PKL+vQqmyNdX2h7k0UDLm237dlMAE02Zd4iHZCis0lBLX8eh gCgeg73yDrPMGxl3wJl0/HwD0L7CvN8RMalMoCMLJ/TndVu1ow9tfRk0TZFfeJInElSodSwkCw6 hQcY5c1NmOA801TkKow3EfHFPIES6+/SEjXiwoZyB3nlL X-Received: by 2002:a05:600c:a302:b0:4bb:7347:d5c0 with SMTP id 5b1f17b1804b1-4bb7347d930mr30750755e9.9.1794827419398; Mon, 16 Nov 2026 03:10:19 -0800 (PST) X-Received: by 2002:a05:600c:a302:b0:4bb:7347:d5c0 with SMTP id 5b1f17b1804b1-4bb7347d930mr30750005e9.9.1794827418567; Mon, 16 Nov 2026 03:10:18 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb55ae501bsm303707585e9.10.2026.11.16.03.10.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2026 03:10:18 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 3/5] gdbserver: improve packet parsing in tracepoint.cc Date: Mon, 16 Nov 2026 11:10:09 +0000 Message-Id: <3744f1d637c3214c7ad67e627d608ddcad31e290.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: -nQvPXXm5KrnwNf-XLyiWtzkDI4GRW3ao77Oa1imA7M_1794827420 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 The current packet parsing code in gdbserver/tracepoint.cc has code like this: ++packet; /* skip a colon */ Where PACKET points into the incoming packet buffer. There is no validation that *PACKET actually points at a colon character. This means that incorrectly formatted incoming packets might be accepted with unexpected results. For example, *PACKET might be the end of packet null character, in which case gdbserver will skip the null and start processing garbage bytes as packet data. This commit introduces check_char_and_skip_or_write_error which wraps a common pattern; check if *PACKET is a specific character, and if it is, move PACKET past that character. If *PACKET is not the expected character then write an error into the outgoing packet buffer and return. I then make use of this function throughout tracepoint.cc to improve the packet parsing. There are two other, similar fixes included in this commit. In cmd_qtdp we need to check for either 'E' or 'D'; as check_char_and_skip_or_write_error only checks for one named character, I wrote that check inline. And in cmd_qtdpsrc and cmd_qtnotes we use strchr to search for the next ':' or ';' character. However, if the character is missing then we will get back nullptr. Currently the code doesn't check for this and just continues. In these cases I've introduced a nullptr check and early return. Additionally in cmd_qtdpsrc, at the new early return sites, I've added an xfree call to release a buffer that is allocated earlier in the function with xmalloc. This buffer would be better managed using gdb::unique_xmalloc_ptr. I have left switching to gdb::unique_xmalloc_ptr until a later commit in order to keep this commit more focused. So for now we gain some xfree calls, but these will be removed by the end of this series. There are new tests covering the new checks. --- .../gdb.server/invalid-tracepoint-packets.exp | 295 +++++++++++++++++- gdbserver/tracepoint.cc | 125 +++++++- 2 files changed, 395 insertions(+), 25 deletions(-) diff --git a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp index d82647cbe63..de37d657b57 100644 --- a/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp +++ b/gdb/testsuite/gdb.server/invalid-tracepoint-packets.exp @@ -63,12 +63,10 @@ proc start_gdbserver_for_test {} { return true } -# Test the QTDPsrc packet. -proc_with_prefix test_qtdpsrc {} { - if {![start_gdbserver_for_test]} { - return - } - +# Disassemble the main function and return a list containing the addresses +# of each instruction. The returned addresses have the leading 0x and any +# leading 0s stripped off. +proc disassemble_main_for_addresses {} { # Disassemble main and find an address at which we could place a # tracepoint. set addresses {} @@ -85,8 +83,74 @@ proc_with_prefix test_qtdpsrc {} { } } - # We need at least one address in main for this test. - if {[llength $addresses] < 1} { + return $addresses +} + +# Having sent gdbserver a tracepoint related packet that contains a +# non-hex character in a place where only hex characters are allowed, +# we currently expect to see gdbserver exit after printing an error +# about invalid hex digits. Check for the error message and check +# gdbserver exits. +# +# Before returning, clean up the gdbserver state now that it is no +# longer running. +proc ensure_gdbserver_exited_due_to_non_hex {} { + # Check the gdbserver exited as we expect. + with_spawn_id $::server_spawn_id { + set saw_invalid_hex_msg false + gdb_test_multiple "" "gdbserver exited" -lbl { + -re "\r\ngdbserver: Invalid hex digit \[^\r\n\]+(?=\r\n)" { + set saw_invalid_hex_msg true + exp_continue + } + eof { + gdb_assert { $saw_invalid_hex_msg } $gdb_test_name + } + timeout { + fail $gdb_test_name + } + } + } + + # Clean up gdbserver state. + close_gdbserver +} + +# Test the QTDV packet. +proc_with_prefix test_qtdv {} { + if {![start_gdbserver_for_test]} { + return + } + + # Tests with missing fields of the packet. + gdb_test "maint packet QTDV:2" \ + $::error_re "missing colon after number" + + gdb_test "maint packet QTDV:2:5" \ + $::error_re "missing colon after value" + + gdb_test "maint packet QTDV:2:5:0" \ + $::error_re "missing colon after builtin flag" + + # Send a valid QTDV packet to ensure valid packets are accepted. + gdb_test "maint packet QTDV:2:7:0:616263" $::ok_re \ + "valid QTDV packet is accepted" + + gdbserver_exit false +} + +# Test the QTDPsrc packet. +proc_with_prefix test_qtdpsrc {} { + if {![start_gdbserver_for_test]} { + return + } + + # Disassemble main and find an address at which we could place a + # tracepoint. + set addresses [disassemble_main_for_addresses] + + # We need at least 2 different addresses in main for this test. + if {[llength $addresses] < 2} { unsupported "not enough instruction addresses in main" gdbserver_exit false return @@ -94,6 +158,7 @@ proc_with_prefix test_qtdpsrc {} { # Record the address we'll use in this test. set addr [lindex $addresses 0] + set other_addr [lindex $addresses 1] # Create a tracepoint at ADDR. Do this manually as the rest of this # test also does manual packet manipulation. @@ -129,13 +194,227 @@ proc_with_prefix test_qtdpsrc {} { $::error_re "string of $count nibbles and declared length 1 byte" } + # Tests with missing fields of the packet. + gdb_test "maint packet QTDPsrc:1234" \ + $::error_re "missing colon after number" + + gdb_test "maint packet QTDPsrc:1:$addr" \ + $::error_re "missing colon after address" + + gdb_test "maint packet QTDPsrc:1:$addr:cond" \ + $::error_re "missing colon after type" + + gdb_test "maint packet QTDPsrc:1:$addr:cond:0" \ + $::error_re "missing colon after start" + + gdb_test "maint packet QTDPsrc:1:$addr:cond:0:1" \ + $::error_re "missing colon after length" + + # Test with an invalid tracepoint number. + gdb_test "maint packet QTDPsrc:2:$addr:cond:0:1:41" \ + $::error_re "invalid tracepoint number" + + # Test with a valid tracepoint number, but the wrong address. + gdb_test "maint packet QTDPsrc:1:$other_addr:cond:0:1:41" \ + $::error_re "invalid tracepoint address" + # Test some valid packets. gdb_test "maint packet ${prefix}:1:41" $::ok_re "valid string accepted" gdb_test "maint packet ${prefix}:3:414243" $::ok_re \ "valid multi-byte string accepted" gdb_test "maint packet ${prefix}:0:" $::ok_re "empty string accepted" + # Test a packet containing a string encoding with non-hex characters. + # Currently this will cause gdbserver to exit as we start with --once. + # This behaviour could probably be changed in the future as exiting + # gdbserver is a pretty extreme reaction to bad packet data. But for + # now this test ensures that the packet is handled as we expect. + gdb_test "maint packet ${prefix}:1:zz" \ + $::error_re "string encoding is non-hex" + + ensure_gdbserver_exited_due_to_non_hex +} + +# Test QTDP packet. +proc_with_prefix test_qtdp {} { + if {![start_gdbserver_for_test]} { + return + } + + set addresses [disassemble_main_for_addresses] + if {[llength $addresses] < 1} { + unsupported "not enough instruction addresses in main" + gdbserver_exit false + return + } + + set addr [lindex $addresses 0] + + # Send a valid packet to ensure valid packets are accepted. + gdb_test "maint packet QTDP:1:$addr:D:1:1" $::ok_re "create tracepoint" + + # Send packets with missing fields. + gdb_test "maint packet QTDP:1" $::error_re "missing colon after number" + gdb_test "maint packet QTDP:1:$addr" $::error_re \ + "missing colon after address" + gdb_test "maint packet QTDP:1:$addr:D" $::error_re \ + "missing colon after enable/disable field" + gdb_test "maint packet QTDP:1:$addr:D:1" $::error_re \ + "missing colon after step count" + + # Packets with invalid enable/disable field. + gdb_test "maint packet QTDP:1:$addr:X:1:1" $::error_re \ + "invalid enable/disable field" + gdbserver_exit false } +# Test QTEnable and QTDisable packets. +proc_with_prefix test_qtenable_qtdisable {} { + if {![start_gdbserver_for_test]} { + return + } + + set addresses [disassemble_main_for_addresses] + if {[llength $addresses] < 1} { + unsupported "not enough instruction addresses in main" + gdbserver_exit false + return + } + set addr [lindex $addresses 0] + + # Create a tracepoint. + gdb_test "maint packet QTDP:2:$addr:D:1:1" $::ok_re "create tracepoint" + + # Send enable/disable packets with fields missing. + gdb_test "maint packet QTEnable:2" $::error_re \ + "missing colon after number for enable" + gdb_test "maint packet QTDisable:2" $::error_re \ + "missing colon after number for disable" + + # Send a valid packet to ensure these work. + gdb_test "maint packet QTEnable:2:$addr" $::ok_re \ + "enable tracepoint" + gdb_test "maint packet QTDisable:2:$addr" $::ok_re \ + "disable tracepoint" + + gdbserver_exit false +} + +# Test QTro packet. +proc_with_prefix test_qtro {} { + if {![start_gdbserver_for_test]} { + return + } + + set addresses [disassemble_main_for_addresses] + if {[llength $addresses] < 2} { + unsupported "not enough instruction addresses in main" + gdbserver_exit false + return + } + + set start [lindex $addresses 0] + set end [lindex $addresses 1] + + # Send packets with missing fields. + gdb_test "maint packet QTro:$start" $::error_re \ + "missing comma after start" + gdb_test "maint packet QTro:$start,$end:$start" $::error_re \ + "missing comma after second start" + + # Send a valid packet to ensure valid packets are accepted. + gdb_test "maint packet QTro:$start,$end" $::ok_re \ + "valid packet, single range" + gdb_test "maint packet QTro:$start,$end:$start,$end" $::ok_re \ + "valid packet, two ranges" + + gdbserver_exit false +} + +# Test qTP packet. +proc_with_prefix test_qtp {} { + if {![start_gdbserver_for_test]} { + return + } + + set addresses [disassemble_main_for_addresses] + if {[llength $addresses] < 1} { + unsupported "not enough instruction addresses in main" + gdbserver_exit false + return + } + set addr [lindex $addresses 0] + + # Create a tracepoint. + gdb_test "maint packet QTDP:2:$addr:D:1:1" $::ok_re "create tracepoint" + + # Send qTP packet with fields missing. + gdb_test "maint packet qTP:2" $::error_re "missing colon after number" + + # Send a valid packet to check this can work. + gdb_test "maint packet qTP:2:$addr" \ + "\r\nreceived: \"V${::decimal}:${::decimal}\"" \ + "valid packet" + + gdbserver_exit false +} + +# Test QTBuffer packet. +proc_with_prefix test_qtbuffer {} { + if {![start_gdbserver_for_test]} { + return + } + + # Send qTBuffer packets with missing fields. + gdb_test "maint packet qTBuffer:2" $::error_re \ + "missing comma after offset" + + # Send a valid qTBuffer packet. Request content at offset 0. As + # we have not collected any trace data by this point we should get + # back an 'l' indicating that we are at the end of the trace buffer. + gdb_test "maint packet qTBuffer:0,10" \ + "\r\nreceived: \"l\"" \ + "valid qTBuffer" + + gdbserver_exit false +} + +# Test the QTNotes packet. +proc_with_prefix test_qtnotes {} { + if {![start_gdbserver_for_test]} { + return + } + + # Test the valid QTNotes packet case. + gdb_test "maint packet QTNotes:user:414243;notes:444546;tstop:47;" \ + $::ok_re "valid QTNotes packet accepted" + + # A second packet replaces the existing notes. + gdb_test "maint packet QTNotes:user:484950;notes:515253;tstop:54;" \ + $::ok_re "second valid QTNotes packet accepted" + + # Packets with missing field separators. + gdb_test "maint packet QTNotes:user:4142" $::error_re \ + "missing semicolon after user field" + gdb_test "maint packet QTNotes:notes:4243" $::error_re \ + "missing semicolon after notes field" + gdb_test "maint packet QTNotes:tstop:44" $::error_re \ + "missing semicolon after tstop field" + + # The invalid packets should not have updated the stored data. + gdb_test "maint packet qTStatus"\ + "received: \"\[^\r\n\]*username:484950;notes:515253\[^\r\n\]*\"" \ + "check notes with qTStatus packet" + + gdbserver_exit false +} + +test_qtdv test_qtdpsrc +test_qtdp +test_qtenable_qtdisable +test_qtro +test_qtp +test_qtbuffer +test_qtnotes diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc index 00cbec8c8da..7f0b233fa43 100644 --- a/gdbserver/tracepoint.cc +++ b/gdbserver/tracepoint.cc @@ -2335,6 +2335,29 @@ clear_installed_tracepoints (void) target_unpause_all (true); } +/* OWN_BUF is the complete packet buffer. *PTR is a pointer to a character + within the buffer. Check that **PTR points at character CH. If it does + then increment *PTR to skip the character and return true. + + Otherwise, if **PTR is not character CH, write an error into OWN_BUF and + return false. */ + +static bool +check_char_and_skip_or_write_error (char *own_buf, const char **ptr, char ch) +{ + if (**ptr != ch) + { + trace_debug ("Tracepoint error: missing '%c' at offset %s in: %s", + ch, pulongest (*ptr - own_buf), own_buf); + write_enn (own_buf); + return false; + } + + /* Skip the CH character. */ + (*ptr)++; + return true; +} + /* Parse a packet that defines a tracepoint. */ static void @@ -2353,9 +2376,11 @@ cmd_qtdp (char *own_buf) ULONGEST num, addr; packet = unpack_varlen_hex (packet, &num); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; packet = unpack_varlen_hex (packet, &addr); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; /* See if we already have this tracepoint. */ struct tracepoint *tpoint = find_tracepoint (num, addr); @@ -2377,14 +2402,34 @@ cmd_qtdp (char *own_buf) tpoint = add_tracepoint (num, addr); + /* Check the ENA field is either 'E' or 'D'. */ + if (*packet != 'E' && *packet != 'D') + { + trace_debug ("Tracepoint error: missing 'E' or 'D' at offset %s in: %s", + pulongest (packet - own_buf), own_buf); + write_enn (own_buf); + remove_tracepoint (tpoint); + return; + } + tpoint->enabled = (*packet == 'E'); - ++packet; /* skip 'E' */ - ++packet; /* skip a colon */ + /* Skip the 'E' or 'D'. */ + ++packet; + + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + { + remove_tracepoint (tpoint); + return; + } ULONGEST count; packet = unpack_varlen_hex (packet, &count); tpoint->step_count = count; - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + { + remove_tracepoint (tpoint); + return; + } packet = unpack_varlen_hex (packet, &count); tpoint->pass_count = count; /* See if we have any of the additional optional fields. */ @@ -2510,9 +2555,11 @@ cmd_qtdpsrc (char *own_buf) ULONGEST num, addr; packet = unpack_varlen_hex (packet, &num); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; packet = unpack_varlen_hex (packet, &addr); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; /* See if we already have this tracepoint. */ struct tracepoint *tpoint = find_tracepoint (num, addr); @@ -2527,6 +2574,12 @@ cmd_qtdpsrc (char *own_buf) /* The start of the string type field within PACKET. */ const char *start_of_type = packet; packet = strchr (packet, ':'); + if (packet == nullptr) + { + trace_debug ("Tracepoint error: missing ':' in: %s", 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'; @@ -2542,9 +2595,17 @@ cmd_qtdpsrc (char *own_buf) check SLEN against the remaining packet length. */ ULONGEST start, slen; packet = unpack_varlen_hex (packet, &start); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + { + xfree (srctype); + return; + } packet = unpack_varlen_hex (packet, &slen); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + { + xfree (srctype); + return; + } size_t len = strlen (packet); if (slen != len / 2 || len % 2 != 0) @@ -2597,11 +2658,14 @@ cmd_qtdv (char *own_buf) ULONGEST num, val, builtin; packet = unpack_varlen_hex (packet, &num); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; packet = unpack_varlen_hex (packet, &val); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; packet = unpack_varlen_hex (packet, &builtin); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; size_t nbytes = strlen (packet) / 2; char *varname = (char *) xmalloc (nbytes + 1); @@ -2627,7 +2691,8 @@ cmd_qtenable_disable (char *own_buf, bool enable) ULONGEST num, addr; packet = unpack_varlen_hex (packet, &num); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; packet = unpack_varlen_hex (packet, &addr); struct tracepoint *tp = find_tracepoint (num, addr); @@ -2739,11 +2804,17 @@ cmd_qtro (char *own_buf) while (*packet == ':') { - ++packet; /* skip a colon */ + ++packet; ULONGEST start, end; packet = unpack_varlen_hex (packet, &start); - ++packet; /* skip a comma */ + + /* The character *PACKET should be a comma. If it is not then write + an error into OWN_BUF and return. If there were multiple regions + in this packet then some might already have been set by this + point. The next QTro packet will clear the region list. */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ',')) + return; packet = unpack_varlen_hex (packet, &end); struct readonly_region *roreg = XNEW (struct readonly_region); @@ -3485,7 +3556,8 @@ cmd_qtp (char *own_buf) ULONGEST num, addr; packet = unpack_varlen_hex (packet, &num); - ++packet; /* skip a colon */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ':')) + return; packet = unpack_varlen_hex (packet, &addr); /* See if we already have this tracepoint. */ @@ -3738,7 +3810,8 @@ cmd_qtbuffer (char *own_buf) ULONGEST offset, num; packet = unpack_varlen_hex (packet, &offset); - ++packet; /* skip a comma */ + if (!check_char_and_skip_or_write_error (own_buf, &packet, ',')) + return; unpack_varlen_hex (packet, &num); trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s", @@ -3837,6 +3910,12 @@ cmd_qtnotes (char *own_buf) packet += strlen ("user:"); const char *start_of_text = packet; packet = strchr (packet, ';'); + if (packet == nullptr) + { + trace_debug ("Tracepoint error: missing ';' in: %s", own_buf); + write_enn (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); @@ -3851,6 +3930,12 @@ cmd_qtnotes (char *own_buf) packet += strlen ("notes:"); const char *start_of_text = packet; packet = strchr (packet, ';'); + if (packet == nullptr) + { + trace_debug ("Tracepoint error: missing ';' in: %s", own_buf); + write_enn (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); @@ -3865,6 +3950,12 @@ cmd_qtnotes (char *own_buf) packet += strlen ("tstop:"); const char *start_of_text = packet; packet = strchr (packet, ';'); + if (packet == nullptr) + { + trace_debug ("Tracepoint error: missing ';' in: %s", own_buf); + write_enn (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); -- 2.25.4