Re: [RFC] Fix BFD leak in solib-darwin.c

John Baldwin
2019-02-22T21:43:18Z

On 2/22/19 9:16 AM, John Baldwin wrote:
> On 2/22/19 8:36 AM, Tom Tromey wrote:
>> commit 192b62ce0b4bb5c61188f570e127a26d2c32f716 ("Use class to manage
>> BFD reference counts") changed darwin_get_dyld_bfd to use:
>>
>> + dyld_bfd.release ();
>>
>> rather than
>>
>> - do_cleanups (cleanup);
>>
>> However, using release here leaks the BFD. Instead I believe reset
>> should be used instead.
>>
>> I can't readily test this, so please take a look and let me know what
>> you think.
>>
>> gdb/ChangeLog
>> 2019-02-22 Tom Tromey <tromey@adacore.com>
>>
>> * solib-darwin.c (darwin_get_dyld_bfd): Use reset, not release.
>> ---
>> gdb/ChangeLog | 4 ++++
>> gdb/solib-darwin.c | 2 +-
>> 2 files changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
>> index d3060604bad..da410ad4660 100644
>> --- a/gdb/solib-darwin.c
>> +++ b/gdb/solib-darwin.c
>> @@ -455,7 +455,7 @@ darwin_get_dyld_bfd ()
>> if (sub != NULL)
>> dyld_bfd = sub;
>> else
>> - dyld_bfd.release ();
>> + dyld_bfd.reset (nullptr);
>> }
>> return dyld_bfd;
>> }
>
> I haven't tested, but this LGTM. Simon has noticed several instances of
> this bug where release() was used instead of reset(). (The most recent one
> was in the build-id separate debug file code that I think hasn't been
> committed yet.) It might be worth doing a quick sweep of current 'release'
> calls (if there aren't too many) to check for other leaks.

I did a quick look and this is the only other one I could find that I think
is also a leak:

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 98f46e0416..055cbc8073 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -2722,7 +2722,7 @@ dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
if (dwz_bfd != NULL)
{
if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
- dwz_bfd.release ();
+ dwz_bfd.reset (nullptr);
}

if (dwz_bfd == NULL)

--
John Baldwin

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â