public inbox for binutils-cvs@sourceware.org
 help / color / mirror / Atom feed
* [binutils-gdb] Memory leaks in bfd/vms-lib.c
@ 2023-06-07  2:02 Alan Modra
  0 siblings, 0 replies; only message in thread
From: Alan Modra @ 2023-06-07  2:02 UTC (permalink / raw)
  To: bfd-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0a22a8f27df7d092620dfa1f0135a2dd7b44528a

commit 0a22a8f27df7d092620dfa1f0135a2dd7b44528a
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Jun 7 10:53:36 2023 +0930

    Memory leaks in bfd/vms-lib.c
    
            * vms-lib.c (vms_lib_read_index): Free malloc'd memory on error
            return paths.
            (vms_write_index, _bfd_vms_lib_write_archive_contents): Likewise.

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 = bfd_alloc (abfd, csm.nbr * sizeof (struct carsym));
       if (csbuf == NULL)
-	return NULL;
+	{
+	  free (csm.idx);
+	  return NULL;
+	}
       memcpy (csbuf, csm.idx, csm.nbr * sizeof (struct carsym));
       free (csm.idx);
       csm.idx = csbuf;
@@ -1745,13 +1748,13 @@ vms_write_index (bfd *abfd,
 		      if (kbn_vbn != 0)
 			{
 			  if (!vms_write_block (abfd, kbn_vbn, kbn_blk))
-			    return false;
+			    goto err;
 			}
 		      else
 			{
 			  kbn_blk = bfd_malloc (VMS_BLOCK_SIZE);
 			  if (kbn_blk == NULL)
-			    return false;
+			    goto err;
 			}
 		      *(unsigned short *)kbn_blk = 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;
 		}
 
 	      /* 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;
 
       free (rblk[j]);
+      rblk[j] = NULL;
     }
 
   /* Write the last kbn (if any).  */
   if (kbn_vbn != 0)
     {
       if (!vms_write_block (abfd, kbn_vbn, kbn_blk))
-	return false;
+	goto err;
       free (kbn_blk);
     }
 
   return true;
+
+ err:
+  if (abfd != NULL)
+    {
+      for (j = 0; j < level; j++)
+	free (rblk[j]);
+      free (kbn_blk);
+    }
+  return false;
 }
 
 /* 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 = NULL;
   struct lib_tdata *tdata = bfd_libdata (arch);
   unsigned int i;
   file_ptr off;
@@ -2173,18 +2186,18 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
 
   /* Create symbol index.  */
   if (!_bfd_vms_lib_build_map (nbr_modules, modules, &nbr_symbols, &symbols))
-    return false;
+    goto err;
 
   vbn = 0;
   if (!vms_write_index (NULL, symbols, nbr_symbols, &vbn, NULL, is_elfidx))
-    return false;
+    goto err;
   nbr_sym_iblk = vbn;
 
   /* Write modules and remember their position.  */
   off = (1 + nbr_mod_iblk + nbr_sym_iblk) * VMS_BLOCK_SIZE;
 
   if (bfd_seek (arch, off, SEEK_SET) != 0)
-    return false;
+    goto err;
 
   for (i = 0; i < nbr_modules; i++)
     {
@@ -2304,10 +2317,10 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
   vbn = 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;
 
   /* Write libary header.  */
   {
@@ -2378,13 +2391,15 @@ _bfd_vms_lib_write_archive_contents (bfd *arch)
     idd++;
 
     if (!vms_write_block (arch, 1, blk))
-      return false;
+      goto err;
   }
 
   return true;
 
  input_err:
   bfd_set_input_error (current, bfd_get_error ());
+ err:
+  free (symbols);
   return false;
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-06-07  2:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07  2:02 [binutils-gdb] Memory leaks in bfd/vms-lib.c Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).