From patchwork Thu Jun 11 21:41:19 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Seitz X-Patchwork-Id: 7130 Received: (qmail 11297 invoked by alias); 11 Jun 2015 21:41:22 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 11285 invoked by uid 89); 11 Jun 2015 21:41:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.3 required=5.0 tests=AWL, BAYES_20, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 11 Jun 2015 21:41:20 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id B4715BC92C for ; Thu, 11 Jun 2015 21:41:19 +0000 (UTC) Received: from valrhona.uglyboxes.com (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t5BLfJU0015422 for ; Thu, 11 Jun 2015 17:41:19 -0400 From: Keith Seitz To: gdb-patches@sourceware.org Subject: [PATCH] Fix lrealpath memory leak in build_id_to_debug_bfd Date: Thu, 11 Jun 2015 14:41:19 -0700 Message-Id: <1434058879-32602-1-git-send-email-keiths@redhat.com> X-IsSubscribed: yes Valgrind reports memory leaking from build_id_to_debug_bfd: ==7261== 88 bytes in 2 blocks are definitely lost in loss record 31,319 of 35,132 ==7261== at 0x4A06BCF: malloc (vg_replace_malloc.c:296) ==7261== by 0x32CA88A9B9: strdup (strdup.c:42) ==7261== by 0xFE62AB: lrealpath (lrealpath.c:88) ==7261== by 0x7F7AD6: build_id_to_debug_bfd (build-id.c:116) ==7261== by 0x7F7BB5: find_separate_debug_file_by_buildid (build-id.c:149) ==7261== by 0x6D9382: elf_symfile_read (elfread.c:1348) ==7261== by 0x777F02: read_symbols (symfile.c:875) ==7261== by 0x778505: syms_from_objfile_1 (symfile.c:1078) ==7261== by 0x778548: syms_from_objfile (symfile.c:1094) ==7261== by 0x778746: symbol_file_add_with_addrs (symfile.c:1191) ==7261== by 0x77893B: symbol_file_add_from_bfd (symfile.c:1280) ==7261== by 0x8E51E3: solib_read_symbols (solib.c:706) ==7261== by 0x8E58AF: solib_add (solib.c:1029) This occurs because commit 1be5090b in bfd, addressing PR 11983, started taking a copy of the input filename instead of directly caching it. It appears that this code was never updated to reflect that API change. This simple patch creates a cleanup to free the return value for lrealpath. gdb/ChangeLog * build-id.c (build_id_to_debug_bfd): Add cleanup to free return value from lrealpath. --- gdb/ChangeLog | 5 +++++ gdb/build-id.c | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 9075fcc..9fd417b 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2015-06-11 Keith Seitz + + * build-id.c (build_id_to_debug_bfd): Add cleanup to free + return value from lrealpath. + 2015-06-11 Gary Benson * nat/linux-namespaces.c (mnsh_send_message): Use pulongest. diff --git a/gdb/build-id.c b/gdb/build-id.c index 8f7bbb4..8f8c2f3 100644 --- a/gdb/build-id.c +++ b/gdb/build-id.c @@ -96,6 +96,7 @@ build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id) size_t size = build_id_len; char *s; char *filename = NULL; + struct cleanup *inner; memcpy (link, debugdir, debugdir_len); s = &link[debugdir_len]; @@ -119,7 +120,10 @@ build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id) continue; /* We expect to be silent on the non-existing files. */ + inner = make_cleanup (xfree, filename); abfd = gdb_bfd_open (filename, gnutarget, -1); + do_cleanups (inner); + if (abfd == NULL) continue;