bfd_get_full_section_contents memory leak, plus

Tom Tromey tromey@redhat.com
Thu Oct 18 18:40:00 GMT 2012


>>>>> "Alan" == Alan Modra <amodra@gmail.com> writes:

Alan> You're correct.  Sorry, I didn't look closely enough at the code.

No problem, thank you for taking another look.

Alan> I guess the reason for the internal decompress buffer is to save time
Alan> decompressing in situations where the section is read more than once.
Alan> I think it would be better to just decompress over again, and leave
Alan> any caching to the caller.  After all, for non-compressed sections
Alan> we read the data off disk again.

That fixes both my issues nicely.

The appended removes the caching for the uncompression case.  I left the
COMPRESS_SECTION_DONE code in place, because it seems to me that a BFD
user could possibly compress section data in place
(bfd_compress_section_contents still sets sec->contents, and as gdb
doesn't use this code I was reluctant to touch it), and then later call
bfd_get_full_section_contents.

Tom

    	* compress.c (bfd_get_full_section_contents): Don't cache
    	decompressed contents.

diff --git a/bfd/compress.c b/bfd/compress.c
index 52c884c..bc14f2c 100644
--- a/bfd/compress.c
+++ b/bfd/compress.c
@@ -165,7 +165,6 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
   bfd_size_type uncompressed_size;
   bfd_size_type rawsize;
   bfd_byte *compressed_buffer;
-  bfd_byte *uncompressed_buffer;
 #endif
 
   if (abfd->direction != write_direction && sec->rawsize != 0)
@@ -220,24 +219,24 @@ bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
       if (!ret)
 	goto fail_compressed;
 
-      uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
-      if (uncompressed_buffer == NULL)
+      if (p == NULL)
+	p = (bfd_byte *) bfd_malloc (uncompressed_size);
+      if (p == NULL)
 	goto fail_compressed;
 
       if (!decompress_contents (compressed_buffer, compressed_size,
-				uncompressed_buffer, uncompressed_size))
+				p, uncompressed_size))
 	{
 	  bfd_set_error (bfd_error_bad_value);
-	  free (uncompressed_buffer);
+	  free (p);
 	fail_compressed:
 	  free (compressed_buffer);
 	  return FALSE;
 	}
 
       free (compressed_buffer);
-      sec->contents = uncompressed_buffer;
-      sec->compress_status = COMPRESS_SECTION_DONE;
-      /* Fall thru */
+      *ptr = p;
+      return TRUE;
 #endif
 
     case COMPRESS_SECTION_DONE:



More information about the Binutils mailing list