[binutils-gdb] Fix memory leak in bfd_get_debug_link_info_1

Alan Modra amodra@sourceware.org
Thu Mar 30 04:57:36 GMT 2023


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

commit ea7672c10e0b762c02fd40ce94d490b56bc14675
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Mar 29 22:31:15 2023 +1030

    Fix memory leak in bfd_get_debug_link_info_1
    
            * opncls.c (bfd_get_alt_debug_link_info): Don't bother freeing
            after bfd_malloc_and_get_section failure.
            (get_build_id): Likewise.
            (bfd_get_debug_link_info_1): Likewise.  Free section contents
            when crc not present.
            * section.c (bfd_malloc_and_get_section): Document that the
            buffer is NULL on error return.

Diff:
---
 bfd/opncls.c  | 20 +++++++-------------
 bfd/section.c |  2 ++
 2 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/bfd/opncls.c b/bfd/opncls.c
index abea464baa4..81e124457e9 100644
--- a/bfd/opncls.c
+++ b/bfd/opncls.c
@@ -1211,10 +1211,7 @@ bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out)
     return NULL;
 
   if (!bfd_malloc_and_get_section (abfd, sect, &contents))
-    {
-      free (contents);
-      return NULL;
-    }
+    return NULL;
 
   /* CRC value is stored after the filename, aligned up to 4 bytes.  */
   name = (char *) contents;
@@ -1222,7 +1219,10 @@ bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out)
   crc_offset = strnlen (name, size) + 1;
   crc_offset = (crc_offset + 3) & ~3;
   if (crc_offset + 4 > size)
-    return NULL;
+    {
+      free (name);
+      return NULL;
+    }
 
   *crc32 = bfd_get_32 (abfd, contents + crc_offset);
   return name;
@@ -1297,10 +1297,7 @@ bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len,
     return NULL;
 
   if (!bfd_malloc_and_get_section (abfd, sect, & contents))
-    {
-      free (contents);
-      return NULL;
-    }
+    return NULL;
 
   /* BuildID value is stored after the filename.  */
   name = (char *) contents;
@@ -1817,10 +1814,7 @@ get_build_id (bfd *abfd)
     }
 
   if (!bfd_malloc_and_get_section (abfd, sect, & contents))
-    {
-      free (contents);
-      return NULL;
-    }
+    return NULL;
 
   /* FIXME: Paranoia - allow for compressed build-id sections.
      Maybe we should complain if this size is different from
diff --git a/bfd/section.c b/bfd/section.c
index 3b1993b60a5..1e7e8ac7522 100644
--- a/bfd/section.c
+++ b/bfd/section.c
@@ -1600,6 +1600,8 @@ SYNOPSIS
 DESCRIPTION
 	Read all data from @var{section} in BFD @var{abfd}
 	into a buffer, *@var{buf}, malloc'd by this function.
+	Return @code{true} on success, @code{false} on failure in which
+	case *@var{buf} will be NULL.
 */
 
 bool


More information about the Binutils-cvs mailing list