[binutils-gdb] ldelf_before_allocation leak

Alan Modra amodra@sourceware.org
Mon Jan 20 07:55:30 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bf2da5d57d7532855e56538a4a7242c705b1d27d

commit bf2da5d57d7532855e56538a4a7242c705b1d27d
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Jan 20 17:41:56 2025 +1030

    ldelf_before_allocation leak
    
    ldelf_before_allocation is passed the audit and depaudit strings built
    from command line args, then possibly adds to the depaudit string,
    freeing the original.  The new string isn't freed.  Fix this leak by
    keeping the string attached to the static vars.
    
            * ldelf.c (ldelf_before_allocation): Pass char** for audit
            and depaudit.  Adjust uses.
            * ldelf.h (ldelf_before_allocation): Update prototype.
            * gld${EMULATION_NAME}_before_allocation: Update call.

Diff:
---
 ld/emultempl/elf.em | 2 +-
 ld/ldelf.c          | 6 +++---
 ld/ldelf.h          | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
index 777f7204038..9dcb52e7514 100644
--- a/ld/emultempl/elf.em
+++ b/ld/emultempl/elf.em
@@ -170,7 +170,7 @@ fragment <<EOF
 static void
 gld${EMULATION_NAME}_before_allocation (void)
 {
-  ldelf_before_allocation (audit, depaudit, ${ELF_INTERPRETER_NAME});
+  ldelf_before_allocation (&audit, &depaudit, ${ELF_INTERPRETER_NAME});
 }
 
 EOF
diff --git a/ld/ldelf.c b/ld/ldelf.c
index d627b55419b..f56a62780f0 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -1752,7 +1752,7 @@ ldelf_append_to_separated_string (char **to, char *op_arg)
    sections, but before any sizes or addresses have been set.  */
 
 void
-ldelf_before_allocation (char *audit, char *depaudit,
+ldelf_before_allocation (char **audit, char **depaudit,
 			 const char *default_interpreter_name)
 {
   const char *rpath;
@@ -1834,7 +1834,7 @@ ldelf_before_allocation (char *audit, char *depaudit,
 		  }
 
 		if (cp != NULL && *cp != '\0')
-		  ldelf_append_to_separated_string (&depaudit, cp);
+		  ldelf_append_to_separated_string (depaudit, cp);
 
 		cp = more ? ++cp2 : NULL;
 	      }
@@ -1844,7 +1844,7 @@ ldelf_before_allocation (char *audit, char *depaudit,
 
   if (! (bfd_elf_size_dynamic_sections
 	 (link_info.output_bfd, command_line.soname, rpath,
-	  command_line.filter_shlib, audit, depaudit,
+	  command_line.filter_shlib, *audit, *depaudit,
 	  (const char * const *) command_line.auxiliary_filters,
 	  &link_info, &sinterp)))
     einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
diff --git a/ld/ldelf.h b/ld/ldelf.h
index a6498cf2758..e8b7c8c7eb8 100644
--- a/ld/ldelf.h
+++ b/ld/ldelf.h
@@ -30,7 +30,7 @@ extern void ldelf_after_open (int, int, int, int, int, const char *);
 extern bool ldelf_setup_build_id (bfd *);
 extern bool ldelf_setup_package_metadata (bfd *);
 extern void ldelf_append_to_separated_string (char **, char *);
-extern void ldelf_before_allocation (char *, char *, const char *);
+extern void ldelf_before_allocation (char **, char **, const char *);
 extern bool ldelf_open_dynamic_archive
   (const char *, search_dirs_type *, lang_input_statement_type *);
 extern lang_output_section_statement_type *ldelf_place_orphan


More information about the Binutils-cvs mailing list