From patchwork Wed Sep 18 12:41:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 34567 Received: (qmail 75185 invoked by alias); 18 Sep 2019 12:41:21 -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 75159 invoked by uid 89); 18 Sep 2019 12:41:20 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=xfree, *ap, 3778 X-HELO: mail-pg1-f182.google.com Received: from mail-pg1-f182.google.com (HELO mail-pg1-f182.google.com) (209.85.215.182) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Sep 2019 12:41:18 +0000 Received: by mail-pg1-f182.google.com with SMTP id 4so4002553pgm.12; Wed, 18 Sep 2019 05:41:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=JaxnnkqnySDvna+HZvYjcSZK/S/h6nMivxrLKdGxMSk=; b=ku4fWcBIoFSBq7QecDBxESJpDC7wppo5Zt5zkOUt8yyHaGZ/T143jvCPBysQr/je7h 4Y1k6DnXXEwfuEm7xVOHhFsi9qL1gI2f3hbAqCgUNfqzZMs3Yn/+Z2CCJ/8giuuYOSjN d67ByX6Q9bB8/e9icbvdpwA0L3pC6cZlaD4qvbghxjgUqiBkk6C3p6sD3gIVU/IwQa9n v2dRW7UbaVXytCcecNqUC0hBBiWLUkxke9D0HReduBWvDyrbHrNgb2x4mK3G8T7Tl43S Gl0Tx6OaSdyCyYEIATT0i+u0PydPnPk6+vVpfXt8Uxj+j4WIfFK8xN29JOxD4RXXSpKz 1A5g== Return-Path: Received: from bubble.grove.modra.org ([2406:3400:51d:8cc0:fd05:fabc:c0f6:91e4]) by smtp.gmail.com with ESMTPSA id 196sm10388773pfz.99.2019.09.18.05.41.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2019 05:41:16 -0700 (PDT) Received: by bubble.grove.modra.org (Postfix, from userid 1000) id 8FA7180801; Wed, 18 Sep 2019 22:11:12 +0930 (ACST) Date: Wed, 18 Sep 2019 22:11:12 +0930 From: Alan Modra To: binutils@sourceware.org, gdb-patches@sourceware.org Subject: Use bfd_set_filename more Message-ID: <20190918124112.GC3685@bubble.grove.modra.org> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) Fixes a few leaks in bfd and ld. I believe this meets the "obvious patch" commit criterion for gdb so I'll commit the gdb part too. bfd/ * mach-o.c (bfd_mach_o_fat_member_init): Likewise. Replace xstrdup and xmalloc with bfd_strdup and bfd_malloc. Return an error status. Adjust calls. * vms-lib.c (_bfd_vms_lib_get_module): Test mhd->id earlier. Close bfd on failure. Replace xstrdup/bfd_alloc use with bfd_malloc. Use bfd_set_filename. gdb/ * solib-spu.c (spu_bfd_open): Use bfd_set_filename. * spu-linux-nat.c (spu_bfd_open): Likewise. ld/ * emultempl/pe.em (after_open): Use bfd_set_filename. * emultempl/pep.em (after_open): Use bfd_set_filename. diff --git a/bfd/mach-o.c b/bfd/mach-o.c index d02398048c..7d70087c95 100644 --- a/bfd/mach-o.c +++ b/bfd/mach-o.c @@ -5417,7 +5417,7 @@ bfd_mach_o_fat_archive_p (bfd *abfd) ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY. Set arelt_data and origin fields too. */ -static void +static bfd_boolean bfd_mach_o_fat_member_init (bfd *abfd, enum bfd_architecture arch_type, unsigned long arch_subtype, @@ -5426,27 +5426,35 @@ bfd_mach_o_fat_member_init (bfd *abfd, struct areltdata *areltdata; /* Create the member filename. Use ARCH_NAME. */ const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype); + char *filename; if (ap) { /* Use the architecture name if known. */ - abfd->filename = xstrdup (ap->printable_name); + filename = bfd_strdup (ap->printable_name); + if (filename == NULL) + return FALSE; } else { /* Forge a uniq id. */ const size_t namelen = 2 + 8 + 1 + 2 + 8 + 1; - char *name = xmalloc (namelen); - snprintf (name, namelen, "0x%lx-0x%lx", + filename = bfd_malloc (namelen); + if (filename == NULL) + return FALSE; + snprintf (filename, namelen, "0x%lx-0x%lx", entry->cputype, entry->cpusubtype); - abfd->filename = name; } + bfd_set_filename (abfd, filename); areltdata = bfd_zmalloc (sizeof (struct areltdata)); + if (areltdata == NULL) + return FALSE; areltdata->parsed_size = entry->size; abfd->arelt_data = areltdata; abfd->iostream = NULL; abfd->origin = entry->offset; + return TRUE; } bfd * @@ -5502,7 +5510,11 @@ bfd_mach_o_fat_openr_next_archived_file (bfd *archive, bfd *prev) bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype, &arch_type, &arch_subtype); - bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry); + if (!bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry)) + { + bfd_close (nbfd); + return NULL; + } bfd_set_arch_mach (nbfd, arch_type, arch_subtype); @@ -5574,9 +5586,8 @@ bfd_mach_o_fat_extract (bfd *abfd, if (res == NULL) return NULL; - bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e); - - if (bfd_check_format (res, format)) + if (bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e) + && bfd_check_format (res, format)) { BFD_ASSERT (bfd_get_arch_info (res) == arch); return res; diff --git a/bfd/vms-lib.c b/bfd/vms-lib.c index f238ba0016..43addd46ec 100644 --- a/bfd/vms-lib.c +++ b/bfd/vms-lib.c @@ -1297,7 +1297,9 @@ _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx) struct lib_tdata *tdata = bfd_libdata (abfd); bfd *res; file_ptr file_off; - char *name; + const char *name; + char *newname; + size_t namelen; /* Sanity check. */ if (modidx >= tdata->nbr_modules) @@ -1335,18 +1337,22 @@ _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx) if (bfd_bread (buf, tdata->mhd_size, abfd) != tdata->mhd_size) return NULL; + mhd = (struct vms_mhd *) buf; + if (mhd->id != MHD__C_MHDID) + return NULL; + res = _bfd_create_empty_archive_element_shell (abfd); if (res == NULL) return NULL; arelt = bfd_zmalloc (sizeof (*arelt)); if (arelt == NULL) - return NULL; + { + bfd_close (res); + return NULL; + } res->arelt_data = arelt; /* Get info from mhd. */ - mhd = (struct vms_mhd *)buf; - if (mhd->id != MHD__C_MHDID) - return NULL; if (tdata->mhd_size >= offsetof (struct vms_mhd, objstat) + 1) res->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0; res->mtime = vms_rawtime_to_time_t (mhd->datim); @@ -1361,23 +1367,25 @@ _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx) /* Set filename. */ name = tdata->modules[modidx].name; + namelen = strlen (name); + newname = bfd_malloc (namelen + 4 + 1); + if (newname == NULL) + { + bfd_close (res); + return NULL; + } + strcpy (newname, name); switch (tdata->type) { case LBR__C_TYP_IOBJ: case LBR__C_TYP_EOBJ: /* For object archives, append .obj to mimic standard behaviour. */ - { - size_t namelen = strlen (name); - char *name1 = bfd_alloc (res, namelen + 4 + 1); - memcpy (name1, name, namelen); - strcpy (name1 + namelen, ".obj"); - name = name1; - } + strcpy (newname + namelen, ".obj"); break; default: break; } - res->filename = xstrdup (name); + bfd_set_filename (res, newname); tdata->cache[modidx] = res; diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c index 5b97b9bcf6..c5e0acaf02 100644 --- a/gdb/solib-spu.c +++ b/gdb/solib-spu.c @@ -377,8 +377,7 @@ spu_bfd_open (const char *pathname) strcat (buf, original_name); - xfree ((char *)abfd->filename); - abfd->filename = xstrdup (buf); + bfd_set_filename (abfd.get (), xstrdup (buf)); } } diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c index 2f4b4d6946..9dbb633734 100644 --- a/gdb/spu-linux-nat.c +++ b/gdb/spu-linux-nat.c @@ -371,8 +371,7 @@ spu_bfd_open (ULONGEST addr) sect_size - 20); buf[sect_size - 20] = '\0'; - xfree ((char *)nbfd->filename); - nbfd->filename = xstrdup (buf); + bfd_set_filename (nbfd.get (), xstrdup (buf)); } } diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 49bb66b906..2e78f26ad8 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -1543,7 +1543,7 @@ gld_${EMULATION_NAME}_after_open (void) /* Rename this implib to match the other one. */ n = xmalloc (strlen (other_bfd_filename) + 1); strcpy (n, other_bfd_filename); - is->the_bfd->my_archive->filename = n; + bfd_set_filename (is->the_bfd->my_archive, n); } free (relocs); @@ -1648,7 +1648,7 @@ gld_${EMULATION_NAME}_after_open (void) new_name = xmalloc (strlen (is->the_bfd->filename) + 3); sprintf (new_name, "%s.%c", is->the_bfd->filename, seq); - is->the_bfd->filename = new_name; + bfd_set_filename (is->the_bfd, new_name); new_name = xmalloc (strlen (is->filename) + 3); sprintf (new_name, "%s.%c", is->filename, seq); diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index 4daa32b5c6..601ef11c20 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1510,7 +1510,7 @@ gld_${EMULATION_NAME}_after_open (void) /* Rename this implib to match the other one. */ n = xmalloc (strlen (other_bfd_filename) + 1); strcpy (n, other_bfd_filename); - is->the_bfd->my_archive->filename = n; + bfd_set_filename (is->the_bfd->my_archive, n); } free (relocs); @@ -1615,7 +1615,7 @@ gld_${EMULATION_NAME}_after_open (void) new_name = xmalloc (strlen (is->the_bfd->filename) + 3); sprintf (new_name, "%s.%c", is->the_bfd->filename, seq); - is->the_bfd->filename = new_name; + bfd_set_filename (is->the_bfd, new_name); new_name = xmalloc (strlen (is->filename) + 3); sprintf (new_name, "%s.%c", is->filename, seq);