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

Message ID 20260817151646.152571-9-simon.marchi@efficios.com
State New
Headers
Series Fix various warnings when building on macOS |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

Simon Marchi Aug. 17, 2026, 3:16 p.m. UTC
  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(-)
  

Comments

Andrew Burgess Aug. 17, 2026, 4:35 p.m. UTC | #1
Simon Marchi <simon.marchi@efficios.com> writes:

> 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.

Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew
>
> 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
  

Patch

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;
 	}