From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id wZlbBHkmg2q1LC0AWB0awg (envelope-from ) for ; Mon, 17 Aug 2026 11:19:21 -0400 Received: by simark.ca (Postfix, from userid 112) id 0EAF21E033; Mon, 17 Aug 2026 11:19:21 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, 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 97BAC1E033 for ; Mon, 17 Aug 2026 11:19:20 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 45FFC4BA79BB for ; Mon, 17 Aug 2026 15:19:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 45FFC4BA79BB Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id E280F4BA900D; Mon, 17 Aug 2026 15:18:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E280F4BA900D Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=efficios.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=efficios.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E280F4BA900D Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786979934; cv=none; b=ty4JHjRxWPY5l3lvpbN8/1VTf0MkXg2FoC8p0/oY8v9Q/8Mhr57OEl2b6Cv6gTwrufM3E+xPS3BLs1rR7F8/HosBK2pLs9h94kLY58K3PHmhN34XBKJMYc1tnSQV0QSsZEzamn3m4ov7S2F+8f5o+jK9LltMvMMxUtl9aV5ZH+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786979934; c=relaxed/simple; bh=WLLli988CMUdQEJeEe0ercBk/Tmle5bM6AH7k1KNaxA=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=nJpWMN3ZMjYeC3XqWyzRF4r5vcwGT9HB9B7OEsiw0Igz8vogYZOZhVtoEsNSEmDJyJxHgnRXLQgm/SXZ4xPpf5sXP0n7d/Pu3g+d3U1w6dWARpNdRi9ZyNNGd6am0nLmMjfk3CWjaOQx358sFTJMMpimnDMY2IOdGRL6r6ToaEA= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E280F4BA900D Received: by simark.ca (Postfix) id 7E05E1E16F; Mon, 17 Aug 2026 11:18:53 -0400 (EDT) From: Simon Marchi To: gdb-patches@sourceware.org, binutils@sourceware.org Cc: Simon Marchi Subject: [PATCH 12/13] gdb/remote: remove uses of sprintf Date: Mon, 17 Aug 2026 11:16:17 -0400 Message-ID: <20260817151646.152571-13-simon.marchi@efficios.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817151646.152571-1-simon.marchi@efficios.com> References: <20260817151646.152571-1-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 When building on macOS, I get some: /Users/smarchi/src/binutils-gdb/gdb/remote.c:11556:3: error: 'sprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Werror,-Wdeprecated-declarations] 11556 | sprintf (buf, ";cmds:%x,", bp_tgt->persist); | ^ Both are in remote_add_target_side_commands, which unlike the similar remote_add_target_side_condition, does not receive the end of the packet, and does not bound its writes. Give it a BUF_END parameter, and use xsnprintf. The edits to remote_add_target_side_condition are to keep the two functions in sync. Ideally, the pack_hex_byte calls and the `*buf = '\0'` assignments should also have some bound checks, but that is outside the scope of this patch. Change-Id: Ib2e9849d89ebcc9e6275297138a3deb8bf03a7c3 --- gdb/remote.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/gdb/remote.c b/gdb/remote.c index 3d38a9c7c8c9..fe898013a394 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -11513,10 +11513,11 @@ Remote replied unexpectedly while setting startup-with-shell: %s"), } -/* Given a location's target info BP_TGT and the packet buffer BUF, output - the list of conditions (in agent expression bytecode format), if any, the - target needs to evaluate. The output is placed into the packet buffer - started from BUF and ended at BUF_END. */ +/* Given a location's target info BP_TGT and the packet buffer BUF, output the + list of conditions (in agent expression bytecode format), if any, the + target needs to evaluate. + + The output is appended to the existing content of BUF. */ static int remote_add_target_side_condition (struct gdbarch *gdbarch, @@ -11532,35 +11533,42 @@ remote_add_target_side_condition (struct gdbarch *gdbarch, /* Send conditions to the target. */ for (agent_expr *aexpr : bp_tgt->conditions) { - xsnprintf (buf, buf_end - buf, "X%x,", (int) aexpr->buf.size ()); - buf += strlen (buf); + buf += xsnprintf (buf, buf_end - buf, "X%x,", (int) aexpr->buf.size ()); + for (int i = 0; i < aexpr->buf.size (); ++i) buf = pack_hex_byte (buf, aexpr->buf[i]); + *buf = '\0'; } return 0; } +/* Given a location's target info BP_TGT and the packet buffer BUF, output the + list of commands (in agent expression bytecode format), if any, the target + needs to run when the breakpoint is hit. + + The output is appended to the existing content of BUF. */ + static void remote_add_target_side_commands (struct gdbarch *gdbarch, - struct bp_target_info *bp_tgt, char *buf) + struct bp_target_info *bp_tgt, char *buf, + char *buf_end) { if (bp_tgt->tcommands.empty ()) return; buf += strlen (buf); - - sprintf (buf, ";cmds:%x,", bp_tgt->persist); - buf += strlen (buf); + buf += xsnprintf (buf, buf_end - buf, ";cmds:%x,", bp_tgt->persist); /* Concatenate all the agent expressions that are commands into the cmds parameter. */ for (agent_expr *aexpr : bp_tgt->tcommands) { - sprintf (buf, "X%x,", (int) aexpr->buf.size ()); - buf += strlen (buf); + buf += xsnprintf (buf, buf_end - buf, "X%x,", (int) aexpr->buf.size ()); + for (int i = 0; i < aexpr->buf.size (); ++i) buf = pack_hex_byte (buf, aexpr->buf[i]); + *buf = '\0'; } } @@ -11604,7 +11612,7 @@ remote_target::insert_breakpoint (struct gdbarch *gdbarch, remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf); if (can_run_breakpoint_commands ()) - remote_add_target_side_commands (gdbarch, bp_tgt, p); + remote_add_target_side_commands (gdbarch, bp_tgt, p, endbuf); putpkt (rs->buf); getpkt (&rs->buf); @@ -11912,7 +11920,7 @@ remote_target::insert_hw_breakpoint (struct gdbarch *gdbarch, remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf); if (can_run_breakpoint_commands ()) - remote_add_target_side_commands (gdbarch, bp_tgt, p); + remote_add_target_side_commands (gdbarch, bp_tgt, p, endbuf); putpkt (rs->buf); getpkt (&rs->buf); -- 2.55.0