[PATCH 09/13] gdb/elfread: remove use of sprintf

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


When building on macOS, I get:

    /Users/smarchi/src/binutils-gdb/gdb/elfread.c:813:3: 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]
      813 |   sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);
          |   ^

Change this use of sprintf with an std::string, which also allows
getting rid of a use of alloca.

Change-Id: I296e25c863463ef05eca582c8303557570d63cf0
---
 gdb/elfread.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/gdb/elfread.c b/gdb/elfread.c
index e3890ae0270c..fcbd0176d08f 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -804,20 +804,17 @@ static int
 elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
 {
   gnu_ifunc_debug_printf ("resolving \"%s\" by GOT", name);
-  char *name_got_plt;
-  const size_t got_suffix_len = strlen (SYMBOL_GOT_PLT_SUFFIX);
   int found = 0;
   const char *func = __func__;
 
-  name_got_plt = (char *) alloca (strlen (name) + got_suffix_len + 1);
-  sprintf (name_got_plt, "%s" SYMBOL_GOT_PLT_SUFFIX, name);
+  std::string name_got_plt = std::string (name) + SYMBOL_GOT_PLT_SUFFIX;
 
   /* FIXME: we only search the initial namespace.
 
      To search other namespaces, we would need to provide context, e.g. in
      form of an objfile in that namespace.  */
   current_program_space->iterate_over_objfiles_in_search_order
-    ([name, name_got_plt, &addr_p, &found, func] (struct objfile *objfile)
+    ([name, &name_got_plt, &addr_p, &found, func] (struct objfile *objfile)
        {
 	 bfd *obfd = objfile->obfd.get ();
 	 struct gdbarch *gdbarch = objfile->arch ();
@@ -828,8 +825,8 @@ elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
 	 gdb_byte *buf = (gdb_byte *) alloca (ptr_size);
 
 	 bound_minimal_symbol msym
-	   = lookup_minimal_symbol (current_program_space, name_got_plt,
-				    objfile);
+	   = lookup_minimal_symbol (current_program_space,
+				    name_got_plt.c_str (), objfile);
 	 if (msym.minsym == NULL)
 	   return 0;
 	 if (msym.minsym->type () != mst_slot_got_plt)
@@ -850,7 +847,8 @@ elf_gnu_ifunc_resolve_by_got (const char *name, CORE_ADDR *addr_p)
 	 addr = gdbarch_addr_bits_remove (gdbarch, addr);
 
 	 gnu_ifunc_debug_printf_func (func, "GOT entry \"%s\" points to %s",
-				      name_got_plt, paddress (gdbarch, addr));
+				      name_got_plt.c_str (),
+				      paddress (gdbarch, addr));
 
 	 if (elf_gnu_ifunc_record_cache (name, addr))
 	   {
-- 
2.55.0



More information about the Binutils mailing list