bfd_get_full_section_contents memory leak, plus

Alan Modra amodra@gmail.com
Thu Oct 18 04:11:00 GMT 2012


On Wed, Oct 17, 2012 at 07:22:11PM -0600, Tom Tromey wrote:
> >>>>> "Alan" == Alan Modra <amodra@gmail.com> writes:
> 
> Tom> I noticed that bfd_get_full_section_contents has a memory leak.  If you
> Tom> set BFD_DECOMPRESS on the BFD, then use bfd_get_full_section_contents to
> Tom> read a compressed section, the uncompressed section data will be
> Tom> malloc'd and stored as section->contents -- but nothing ever frees this.
> 
> Tom> My fix for this (appended) is to allocate the uncompressed data on the
> Tom> BFD's objalloc.
> 
> Alan> Not OK.  bfd_malloc_and_get_section calls bfd_get_full_section_contents.
> Alan> The data must be malloc'd because users of b_m_a_g_s free it.
> 
> I don't think that is the case -- my patch doesn't affect the 'ptr'
> argument to bfd_get_full_section_contents -- but I will find another way
> to avoid the leak.

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

> I'd also appreciate a comment on my other question.

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

-- 
Alan Modra
Australia Development Lab, IBM



More information about the Binutils mailing list