This is the mail archive of the binutils-cvs@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Use bfd_alloc for compressed section contents


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

commit 030aeb759908d7bcf1b87f3599bfa482a90ab6c1
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Apr 10 03:54:41 2015 -0700

    Use bfd_alloc for compressed section contents
    
    Compressed section contents should be bfd_alloced to avoid memory leak.
    This patch replaces bfd_malloc and free with bfd_alloc and bfd_release
    on compressed buffer in bfd_compress_section_contents.  There is still
    a very small memory leak when compressed section isn't smaller.
    
    	* compress.c (bfd_compress_section_contents): Replace bfd_malloc
    	and free with bfd_alloc and bfd_release on compressed buffer.
    	Release buffer if compressed section isn't smaller.

Diff:
---
 bfd/ChangeLog  | 6 ++++++
 bfd/compress.c | 9 ++++++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index f50ae75..10a3ae2 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,9 @@
+2015-04-10  H.J. Lu  <hongjiu.lu@intel.com>
+
+	* compress.c (bfd_compress_section_contents): Replace bfd_malloc
+	and free with bfd_alloc and bfd_release on compressed buffer.
+	Release buffer if compressed section isn't smaller.
+
 2015-04-10  Alan Modra  <amodra@gmail.com>
 
 	PR ld/18222
diff --git a/bfd/compress.c b/bfd/compress.c
index 7945344..770ea57 100644
--- a/bfd/compress.c
+++ b/bfd/compress.c
@@ -131,7 +131,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
       decompress = FALSE;
       buffer_size = compressed_size + compression_header_size;
     }
-  buffer = (bfd_byte *) bfd_malloc (buffer_size);
+  buffer = (bfd_byte *) bfd_alloc (abfd, buffer_size);
   if (buffer == NULL)
     return 0;
 
@@ -144,7 +144,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
 				    buffer, uncompressed_size))
 	    {
 	      bfd_set_error (bfd_error_bad_value);
-	      free (buffer);
+	      bfd_release (abfd, buffer);
 	      return 0;
 	    }
 	  free (uncompressed_buffer);
@@ -169,7 +169,7 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
 		    (const Bytef*) uncompressed_buffer,
 		    uncompressed_size) != Z_OK)
 	{
-	  free (buffer);
+	  bfd_release (abfd, buffer);
 	  bfd_set_error (bfd_error_bad_value);
 	  return 0;
 	}
@@ -189,6 +189,9 @@ bfd_compress_section_contents (bfd *abfd, sec_ptr sec,
 	}
       else
 	{
+	  /* NOTE: There is a small memory leak here since
+	     uncompressed_buffer is malloced and won't be freed.  */
+	  bfd_release (abfd, buffer);
 	  sec->contents = uncompressed_buffer;
 	  sec->compress_status = COMPRESS_SECTION_NONE;
 	  return uncompressed_size;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]