]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
PR31145, potential memory leak in binutils/ld
authorAlan Modra <amodra@gmail.com>
Fri, 15 Dec 2023 01:54:56 +0000 (12:24 +1030)
committerAlan Modra <amodra@gmail.com>
Fri, 15 Dec 2023 02:26:45 +0000 (12:56 +1030)
PR 31145
* bfd.c (BFD_IN_MEMORY): Mention that bim is malloc'd.
* format.c (io_reinit): Free BFD_IN_MEMORY iostream.
* opncls.c (_bfd_delete_bfd): Likewise.
(bfd_make_readable): Delete unnecessary code.
* bfd-in2.h: Regenerate.

bfd/bfd-in2.h
bfd/bfd.c
bfd/format.c
bfd/opncls.c

index 040d5560cdf78d34a81028e93abe09fa3d72c4a8..2807e694b8f7c88ad3a31014b405e05468edfb88 100644 (file)
@@ -2035,8 +2035,8 @@ struct bfd
 #define BFD_TRADITIONAL_FORMAT    0x400
 
   /* This flag indicates that the BFD contents are actually cached
-     in memory.  If this is set, iostream points to a bfd_in_memory
-     struct.  */
+     in memory.  If this is set, iostream points to a malloc'd
+     bfd_in_memory struct.  */
 #define BFD_IN_MEMORY             0x800
 
   /* This BFD has been created by the linker and doesn't correspond
index 616ded3f4bea2d4dfcbd6b6f1e141dd493d9d661..a5df4ef031145b91288a0ff6598af25c4a01f993 100644 (file)
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -160,8 +160,8 @@ CODE_FRAGMENT
 .#define BFD_TRADITIONAL_FORMAT    0x400
 .
 .  {* This flag indicates that the BFD contents are actually cached
-.     in memory.  If this is set, iostream points to a bfd_in_memory
-.     struct.  *}
+.     in memory.  If this is set, iostream points to a malloc'd
+.     bfd_in_memory struct.  *}
 .#define BFD_IN_MEMORY             0x800
 .
 .  {* This BFD has been created by the linker and doesn't correspond
index 66dc2e7e51e5bdc2d4ea66e055e6c26d5f47e0b8..31aeb52861a72aeadc347dee8f2dd95a1d0ccae8 100644 (file)
@@ -166,7 +166,14 @@ io_reinit (bfd *abfd, struct bfd_preserve *preserve)
         won't do anything unless abfd->iovec is the cache_iovec.  */
       bfd_cache_close (abfd);
       abfd->iovec = preserve->iovec;
-      abfd->iostream = preserve->iostream;
+
+      if (abfd->iostream != preserve->iostream)
+       {
+         if ((abfd->flags & BFD_IN_MEMORY) != 0)
+           free (abfd->iostream);
+         abfd->iostream = preserve->iostream;
+       }
+
       /* Handle in-memory to file backed transition.  */
       if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0
          && (abfd->flags & BFD_IN_MEMORY) != 0
index 5a77562744c77260fbff3c466fda91fc4755fa62..e7b395936080c7cdc2237ebde2d84f0d6b27c653 100644 (file)
@@ -176,6 +176,8 @@ _bfd_delete_bfd (bfd *abfd)
   else
     free ((char *) bfd_get_filename (abfd));
 
+  if ((abfd->flags & BFD_IN_MEMORY) != 0)
+    free (abfd->iostream);
   free (abfd->arelt_data);
   free (abfd);
 }
@@ -1064,7 +1066,6 @@ bfd_make_readable (bfd *abfd)
   abfd->section_count = 0;
   abfd->usrdata = NULL;
   abfd->cacheable = false;
-  abfd->flags |= BFD_IN_MEMORY;
   abfd->mtime_set = false;
 
   abfd->target_defaulted = true;