public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Use bfd_alloc for compressed section contents
@ 2015-04-10  2:38 H.J. Lu
  2015-04-10  7:09 ` Alan Modra
  0 siblings, 1 reply; 2+ messages in thread
From: H.J. Lu @ 2015-04-10  2:38 UTC (permalink / raw)
  To: binutils

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.  I will
check it in tomorrow.


H.J.
---
	* 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.
---
 bfd/ChangeLog  | 6 ++++++
 bfd/compress.c | 9 ++++++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 95cf8d7..3e282fb 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,5 +1,11 @@
 2015-04-09  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-09  H.J. Lu  <hongjiu.lu@intel.com>
+
 	* compress.c (bfd_compress_section_contents): Update comments.
 	(bfd_init_section_decompress_status): Likewise.
 	(bfd_init_section_compress_status): Likewise.
diff --git a/bfd/compress.c b/bfd/compress.c
index 7945344..b48c7ea 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 memeory 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;
-- 
2.1.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Use bfd_alloc for compressed section contents
  2015-04-10  2:38 [PATCH] Use bfd_alloc for compressed section contents H.J. Lu
@ 2015-04-10  7:09 ` Alan Modra
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2015-04-10  7:09 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Thu, Apr 09, 2015 at 07:38:33PM -0700, H.J. Lu wrote:
> 	* 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.

OK.

> +	  /* NOTE: There is a small memeory leak here since

memory

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-04-10  7:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-10  2:38 [PATCH] Use bfd_alloc for compressed section contents H.J. Lu
2015-04-10  7:09 ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).