[binutils-gdb] gdb/dwarf2: 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=554068200b304005f072faa2c7868147b3584b34

commit 554068200b304005f072faa2c7868147b3584b34
Author: Simon Marchi <[email protected]>
Date:   Mon Aug 17 11:16:13 2026 -0400

    gdb/dwarf2: remove uses of sprintf
    
    When building on macOS, I get some:
    
        /Users/smarchi/src/binutils-gdb/gdb/dwarf2/read.c:3773: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]
         3773 |           sprintf (buf, "TU %s at offset %s", hex_string (sig_type->signature),
              |           ^
    
    Replace them with xsnprintf, which takes the destination size and asserts
    that the output was not truncated.
    
    Change-Id: Ie0324f75e5d4aad9b647007848459bf4af5998a6
    Approved-By: Andrew Burgess <[email protected]>

Diff:
---
 gdb/dwarf2/read.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index ca475f53745..a8b99425554 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -3770,15 +3770,16 @@ process_queue (dwarf2_per_objfile *per_objfile)
       if (signatured_type *sig_type = per_cu->as_signatured_type ();
 	  sig_type != nullptr)
 	{
-	  sprintf (buf, "TU %s at offset %s", hex_string (sig_type->signature),
-		   sect_offset_str (per_cu->sect_off ()));
+	  xsnprintf (buf, sizeof (buf), "TU %s at offset %s",
+		     hex_string (sig_type->signature),
+		     sect_offset_str (per_cu->sect_off ()));
 	  /* There can be 100s of TUs.  Only print them in verbose mode.  */
 	  debug_print_threshold = 2;
 	}
       else
 	{
-	  sprintf (buf, "CU at offset %s",
-		   sect_offset_str (per_cu->sect_off ()));
+	  xsnprintf (buf, sizeof (buf), "CU at offset %s",
+		     sect_offset_str (per_cu->sect_off ()));
 	  debug_print_threshold = 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.