[PATCH 08/13] gdb/dwarf2: remove uses of sprintf

Simon Marchi simon.marchi@efficios.com
Mon Aug 17 15:16:13 GMT 2026


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
---
 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 ca475f53745d..a8b99425554a 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;
 	}
 
-- 
2.55.0



More information about the Binutils mailing list