From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 0aARLUcmg2qsLC0AWB0awg (envelope-from ) for ; Mon, 17 Aug 2026 11:18:31 -0400 Received: by simark.ca (Postfix, from userid 112) id B56E71E167; Mon, 17 Aug 2026 11:18:31 -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 4EB7D1E033 for ; Mon, 17 Aug 2026 11:18:31 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 18AAD4BA79BA for ; Mon, 17 Aug 2026 15:18:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18AAD4BA79BA Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 60AF04BA2E3F; Mon, 17 Aug 2026 15:16:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 60AF04BA2E3F 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 60AF04BA2E3F 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=1786979812; cv=none; b=bEr4JOwwSSMMs8PBVA0Vhtc451aN6OB46bxEPs0hX2+ab5QdXMWEt+0YgU+GRK39nitO8bYvZnqEOtDjbFX/aCFhn/iDfxpe9eRQ84fYknTF8zr7twoxwx81WMxill3o30DJNYPngkew+C17QZgTeD0e78WXphusrDV3I+zFzzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1786979812; c=relaxed/simple; bh=QjfYHrLuQySmrSRqG68CsaAOQzxnvhl6TZfNCsDxo7o=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=WwwO9w2vE9EXpuymgjgoij/78KS62FOvsSLffCrvDWn95YQElpmQRd5ORrafVtdyCA2IOSCO5mca22v++wSMl5ye9+XkGPkIXqc3NEwn75wCIIp9Z2lBMd/VDHAfqhZdSKdE0e/kQu2EB6Xx2P7S7/mziooB5EmvEUykImL+pZA= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 60AF04BA2E3F Received: by simark.ca (Postfix) id E5B091E167; Mon, 17 Aug 2026 11:16:51 -0400 (EDT) From: Simon Marchi To: gdb-patches@sourceware.org, binutils@sourceware.org Cc: Simon Marchi Subject: [PATCH 03/13] opcodes/z80: remove use of sprintf Date: Mon, 17 Aug 2026 11:16:08 -0400 Message-ID: <20260817151646.152571-4-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: CC z80-dis.lo /Users/smarchi/src/binutils-gdb/opcodes/z80-dis.c:804:41: 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] 804 | info->fprintf_func = (fprintf_ftype) &sprintf; | ^ Replace this use of sprintf with the safer snprintf. Add a small structure and wrappers around snprintf in order to glue everything together. When asked to review my patch, Claude Code mentioned that the existing code had a latent bug: while info->fprintf_func and info->stream get set temporarily, info->fprintf_styled_func doesn't. If fprintf_styled_func happened to be called, it would receive a `stream` it doesn't expect. It's probably not a problem today, if the disassembler doesn't emit styling, but it seems like a good moment to fix it. Use the disassemble_set_printf function to set both fprintf functions and the stream argument at the same time. Change-Id: I85dee82f3a0c53f38e52ca1158bc605854ab4896 --- opcodes/z80-dis.c | 52 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/opcodes/z80-dis.c b/opcodes/z80-dis.c index d5b4c4210d0c..fd446d3f128d 100644 --- a/opcodes/z80-dis.c +++ b/opcodes/z80-dis.c @@ -768,11 +768,55 @@ pref_ind (struct buffer *buf, disassemble_info *info, const char *txt) static int print_insn_z80_buf (struct buffer *buf, disassemble_info *info); +struct sized_buf +{ + char *buf; + size_t size; +}; + +/* An fprintf_ftype implementation writing to STREAM, which must point to a + struct sized_buf. */ + +static int ATTRIBUTE_PRINTF_2 +sized_buf_printf (void *stream, const char *format, ...) +{ + va_list ap; + int ret; + struct sized_buf *sbuf = (struct sized_buf *) stream; + + va_start (ap, format); + ret = vsnprintf (sbuf->buf, sbuf->size, format, ap); + va_end (ap); + + return ret; +} + +/* Same as sized_buf_printf, but as an fprintf_styled_ftype implementation. + The style is ignored for now. */ + +static int ATTRIBUTE_PRINTF_3 +sized_buf_styled_printf (void *stream, + enum disassembler_style style ATTRIBUTE_UNUSED, + const char *format, ...) +{ + va_list ap; + int ret; + struct sized_buf *sbuf = (struct sized_buf *) stream; + + va_start (ap, format); + ret = vsnprintf (sbuf->buf, sbuf->size, format, ap); + va_end (ap); + + return ret; +} + static int suffix (struct buffer *buf, disassemble_info *info, const char *txt) { char mybuf[TXTSIZ*4]; + struct sized_buf sbuf = { mybuf, sizeof (mybuf) }; fprintf_ftype old_fprintf; + fprintf_styled_ftype old_fprintf_styled; void *old_stream; char *p; @@ -800,15 +844,15 @@ suffix (struct buffer *buf, disassemble_info *info, const char *txt) } old_fprintf = info->fprintf_func; + old_fprintf_styled = info->fprintf_styled_func; old_stream = info->stream; - info->fprintf_func = (fprintf_ftype) &sprintf; - info->stream = mybuf; + disassemble_set_printf (info, &sbuf, sized_buf_printf, + sized_buf_styled_printf); mybuf[0] = 0; buf->base++; if (print_insn_z80_buf (buf, info) >= 0) buf->n_used++; - info->fprintf_func = old_fprintf; - info->stream = old_stream; + disassemble_set_printf (info, old_stream, old_fprintf, old_fprintf_styled); for (p = mybuf; *p; ++p) if (*p == ' ') -- 2.55.0