[binutils-gdb] gdb/tracepoint: remove uses of sprintf

Simon Marchi via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=2452e480ae871587ef4e34ce1da368c71b43b425

commit 2452e480ae871587ef4e34ce1da368c71b43b425
Author: Simon Marchi <[email protected]>
Date:   Mon Aug 17 11:16:18 2026 -0400

    gdb/tracepoint: remove uses of sprintf
    
    When building on macOS, I get some:
    
        /Users/smarchi/src/binutils-gdb/gdb/tracepoint.c:1196:4: 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]
         1196 |           sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
              |           ^
    
    Replace them with xsnprintf.
    
    Change-Id: Id3ec76c47e5c0091fa3a028e36063b2115378e7e
    Approved-By: Andrew Burgess <[email protected]>

Diff:
---
 gdb/tracepoint.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 798bb9a552d..932a8f557e4 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -1164,6 +1164,9 @@ collection_list::stringify ()
     gdb_printf ("\n");
   if (!m_memranges.empty () && info_verbose)
     gdb_printf ("Collecting memranges: \n");
+
+  char *buf_end = temp_buf.data () + temp_buf.size ();
+
   for (i = 0, count = 0, end = temp_buf.data ();
        i < m_memranges.size (); i++)
     {
@@ -1193,11 +1196,12 @@ collection_list::stringify ()
 	   "FFFFFFFF" (or more, depending on sizeof (unsigned)).
 	   Special-case it.  */
 	if (m_memranges[i].type == memrange_absolute)
-	  sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
-		   (long) length);
+	  xsnprintf (end, buf_end - end, "M-1,%s,%lX",
+		     phex_nz (m_memranges[i].start, 0), (long) length);
 	else
-	  sprintf (end, "M%X,%s,%lX", m_memranges[i].type,
-		   phex_nz (m_memranges[i].start, 0), (long) length);
+	  xsnprintf (end, buf_end - end, "M%X,%s,%lX",
+		     m_memranges[i].type, phex_nz (m_memranges[i].start, 0),
+		     (long) length);
       }
 
       count += strlen (end);
@@ -1213,7 +1217,9 @@ collection_list::stringify ()
 	  count = 0;
 	  end = temp_buf.data ();
 	}
-      sprintf (end, "X%08X,", (int) m_aexprs[i]->buf.size ());
+
+      xsnprintf (end, buf_end - end, "X%08X,",
+		 (int) m_aexprs[i]->buf.size ());
       end += 10;		/* 'X' + 8 hex digits + ',' */
       count += 10;
 
@@ -2816,11 +2822,14 @@ encode_source_string (int tpnum, ULONGEST addr,
 {
   if (80 + strlen (srctype) > buf_size)
     error (_("Buffer too small for source encoding"));
-  sprintf (buf, "%x:%s:%s:%x:%x:",
-	   tpnum, phex_nz (addr),
-	   srctype, 0, (int) strlen (src));
+
+  xsnprintf (buf, buf_size, "%x:%s:%s:%x:%x:",
+	     tpnum, phex_nz (addr),
+	     srctype, 0, (int) strlen (src));
+
   if (strlen (buf) + strlen (src) * 2 >= buf_size)
     error (_("Source string too long for buffer"));
+
   bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src));
   return -1;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.