Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org, binutils@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 03/13] opcodes/z80: remove use of sprintf
Date: Mon, 17 Aug 2026 11:16:08 -0400	[thread overview]
Message-ID: <20260817151646.152571-4-simon.marchi@efficios.com> (raw)
In-Reply-To: <20260817151646.152571-1-simon.marchi@efficios.com>

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


  parent reply	other threads:[~2026-08-17 15:18 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-17 15:16 [PATCH 00/13] Fix various warnings when building on macOS Simon Marchi
2026-08-17 15:16 ` [PATCH 01/13] gdbsupport: remove uses of vsprintf Simon Marchi
2026-08-17 15:47   ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 02/13] gdbsupport: remove uses of sprintf Simon Marchi
2026-08-17 15:49   ` Andrew Burgess
2026-08-17 15:16 ` Simon Marchi [this message]
2026-08-18  6:40   ` [PATCH 03/13] opcodes/z80: remove use " Jan Beulich
2026-08-18 16:48     ` Simon Marchi
2026-08-17 15:16 ` [PATCH 04/13] sim/ppc: make defines.h sed command portable Simon Marchi
2026-08-17 15:36   ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 05/13] sim/m32r: fix unused variable warning on non-Linux hosts Simon Marchi
2026-08-17 15:36   ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 06/13] sim/m32r: fix unused function warnings " Simon Marchi
2026-08-17 15:37   ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 07/13] gdb/csky: remove uses of sprintf Simon Marchi
2026-08-17 16:26   ` Andrew Burgess
2026-08-17 17:03     ` Simon Marchi
2026-08-17 20:50     ` Tom Tromey
2026-08-18 18:26       ` Simon Marchi
2026-08-17 15:16 ` [PATCH 08/13] gdb/dwarf2: " Simon Marchi
2026-08-17 16:35   ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 09/13] gdb/elfread: remove use " Simon Marchi
2026-08-17 16:38   ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 10/13] gdbsupport: add xstrcpy Simon Marchi
2026-08-17 16:45   ` Andrew Burgess
2026-08-17 17:30     ` Simon Marchi
2026-08-17 15:16 ` [PATCH 11/13] gdb/remote-fileio: remove uses of sprintf Simon Marchi
2026-08-17 16:53   ` Andrew Burgess
2026-08-17 15:16 ` [PATCH 12/13] gdb/remote: " Simon Marchi
2026-08-17 16:51   ` Andrew Burgess
2026-08-17 17:34     ` Simon Marchi
2026-08-17 15:16 ` [PATCH 13/13] gdb/tracepoint: " Simon Marchi
2026-08-17 16:51   ` Andrew Burgess
2026-08-17 20:52 ` [PATCH 00/13] Fix various warnings when building on macOS Tom Tromey
2026-08-18 18:10 ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260817151646.152571-4-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=binutils@sourceware.org \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox