From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id 880EB385773A; Wed, 7 Jun 2023 02:02:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 880EB385773A Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] Memory leaks in bfd/vms-lib.c X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: c7e6669a5b29240b1e5f6fd2acc987fd75b71044 X-Git-Newrev: 0a22a8f27df7d092620dfa1f0135a2dd7b44528a Message-Id: <20230607020224.880EB385773A@sourceware.org> Date: Wed, 7 Jun 2023 02:02:24 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Jun 2023 02:02:24 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D0a22a8f27df7= d092620dfa1f0135a2dd7b44528a commit 0a22a8f27df7d092620dfa1f0135a2dd7b44528a Author: Alan Modra Date: Wed Jun 7 10:53:36 2023 +0930 Memory leaks in bfd/vms-lib.c =20 * vms-lib.c (vms_lib_read_index): Free malloc'd memory on error return paths. (vms_write_index, _bfd_vms_lib_write_archive_contents): Likewis= e. Diff: --- bfd/vms-lib.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/bfd/vms-lib.c b/bfd/vms-lib.c index 907d0fa75dc..ab938768ebe 100644 --- a/bfd/vms-lib.c +++ b/bfd/vms-lib.c @@ -480,7 +480,10 @@ vms_lib_read_index (bfd *abfd, int idx, unsigned int *= nbrel) the BFD's objalloc. */ csbuf =3D bfd_alloc (abfd, csm.nbr * sizeof (struct carsym)); if (csbuf =3D=3D NULL) - return NULL; + { + free (csm.idx); + return NULL; + } memcpy (csbuf, csm.idx, csm.nbr * sizeof (struct carsym)); free (csm.idx); csm.idx =3D csbuf; @@ -1745,13 +1748,13 @@ vms_write_index (bfd *abfd, if (kbn_vbn !=3D 0) { if (!vms_write_block (abfd, kbn_vbn, kbn_blk)) - return false; + goto err; } else { kbn_blk =3D bfd_malloc (VMS_BLOCK_SIZE); if (kbn_blk =3D=3D NULL) - return false; + goto err; } *(unsigned short *)kbn_blk =3D 0; } @@ -1863,7 +1866,7 @@ vms_write_index (bfd *abfd, { bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used); if (!vms_write_block (abfd, blk[j].vbn, rblk[j])) - return false; + goto err; } =20 /* Reset this block. */ @@ -1956,20 +1959,30 @@ vms_write_index (bfd *abfd, /* Write this block on the disk. */ bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used); if (!vms_write_block (abfd, blk[j].vbn, rblk[j])) - return false; + goto err; =20 free (rblk[j]); + rblk[j] =3D NULL; } =20 /* Write the last kbn (if any). */ if (kbn_vbn !=3D 0) { if (!vms_write_block (abfd, kbn_vbn, kbn_blk)) - return false; + goto err; free (kbn_blk); } =20 return true; + + err: + if (abfd !=3D NULL) + { + for (j =3D 0; j < level; j++) + free (rblk[j]); + free (kbn_blk); + } + return false; } =20 /* Append data to the data block DATA. Force write if PAD is true. */ @@ -2112,7 +2125,7 @@ _bfd_vms_lib_write_archive_contents (bfd *arch) unsigned int nbr_modules; struct lib_index *modules; unsigned int nbr_symbols; - struct lib_index *symbols; + struct lib_index *symbols =3D NULL; struct lib_tdata *tdata =3D bfd_libdata (arch); unsigned int i; file_ptr off; @@ -2173,18 +2186,18 @@ _bfd_vms_lib_write_archive_contents (bfd *arch) =20 /* Create symbol index. */ if (!_bfd_vms_lib_build_map (nbr_modules, modules, &nbr_symbols, &symbol= s)) - return false; + goto err; =20 vbn =3D 0; if (!vms_write_index (NULL, symbols, nbr_symbols, &vbn, NULL, is_elfidx)) - return false; + goto err; nbr_sym_iblk =3D vbn; =20 /* Write modules and remember their position. */ off =3D (1 + nbr_mod_iblk + nbr_sym_iblk) * VMS_BLOCK_SIZE; =20 if (bfd_seek (arch, off, SEEK_SET) !=3D 0) - return false; + goto err; =20 for (i =3D 0; i < nbr_modules; i++) { @@ -2304,10 +2317,10 @@ _bfd_vms_lib_write_archive_contents (bfd *arch) vbn =3D 2; if (!vms_write_index (arch, modules, nbr_modules, &vbn, &mod_idx_vbn, is_elfidx)) - return false; + goto err; if (!vms_write_index (arch, symbols, nbr_symbols, &vbn, &sym_idx_vbn, is_elfidx)) - return false; + goto err; =20 /* Write libary header. */ { @@ -2378,13 +2391,15 @@ _bfd_vms_lib_write_archive_contents (bfd *arch) idd++; =20 if (!vms_write_block (arch, 1, blk)) - return false; + goto err; } =20 return true; =20 input_err: bfd_set_input_error (current, bfd_get_error ()); + err: + free (symbols); return false; }