[binutils-gdb] archive.c bfd_zalloc

Alan Modra amodra@sourceware.org
Mon Dec 30 03:01:00 GMT 2019


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

commit 6a89db5c9513d5e00e02b01095bf0c18e496dcc8
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Dec 30 11:48:20 2019 +1030

    archive.c bfd_zalloc
    
    Quite a few bfd_zalloc calls are wasting time clearing memory, and
    should be bfd_alloc instead.
    
    	* archive.c (do_slurp_bsd_armap): Use bfd_alloc rather than
    	bfd_zalloc when memory is all written after the call.
    	(do_slurp_coff_armap): Likewise.  Set bfd_error on ridiculously
    	large allocations that overflow bfd_size_type.  Use just one
    	bfd_release on error exit.
    	(_bfd_slurp_extended_name_table): Use bfd_alloc for extended_names,
    	clear last byte rather than the entire array.  Use bfd_alloc for
    	string table.  Rearrange and simplify code copying file names.

Diff:
---
 bfd/ChangeLog | 11 +++++++++++
 bfd/archive.c | 46 +++++++++++++++++++++-------------------------
 2 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index a1e5273..186346e 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,14 @@
+2019-12-30  Alan Modra  <amodra@gmail.com>
+
+	* archive.c (do_slurp_bsd_armap): Use bfd_alloc rather than
+	bfd_zalloc when memory is all written after the call.
+	(do_slurp_coff_armap): Likewise.  Set bfd_error on ridiculously
+	large allocations that overflow bfd_size_type.  Use just one
+	bfd_release on error exit.
+	(_bfd_slurp_extended_name_table): Use bfd_alloc for extended_names,
+	clear last byte rather than the entire array.  Use bfd_alloc for
+	string table.  Rearrange and simplify code copying file names.
+
 2019-12-29  Alan Modra  <amodra@gmail.com>
 
 	* vms-alpha.c (_bfd_vms_slurp_egsd): Make base_addr a bfd_vma.
diff --git a/bfd/archive.c b/bfd/archive.c
index 6b7a78c..578df09 100644
--- a/bfd/archive.c
+++ b/bfd/archive.c
@@ -968,7 +968,7 @@ do_slurp_bsd_armap (bfd *abfd)
   if (parsed_size < 4)
     return FALSE;
 
-  raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size);
+  raw_armap = (bfd_byte *) bfd_alloc (abfd, parsed_size);
   if (raw_armap == NULL)
     return FALSE;
 
@@ -1059,16 +1059,22 @@ do_slurp_coff_armap (bfd *abfd)
      bsd-style one in core all at once, for simplicity.  */
 
   if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym))
-    return FALSE;
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return FALSE;
+    }
 
   carsym_size = (nsymz * sizeof (carsym));
   ptrsize = (4 * nsymz);
 
   if (carsym_size + stringsize + 1 <= carsym_size)
-    return FALSE;
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return FALSE;
+    }
 
-  ardata->symdefs = (struct carsym *) bfd_zalloc (abfd,
-						  carsym_size + stringsize + 1);
+  ardata->symdefs = (struct carsym *) bfd_alloc (abfd,
+						 carsym_size + stringsize + 1);
   if (ardata->symdefs == NULL)
     return FALSE;
   carsyms = ardata->symdefs;
@@ -1083,7 +1089,7 @@ do_slurp_coff_armap (bfd *abfd)
     {
       if (bfd_get_error () != bfd_error_system_call)
 	bfd_set_error (bfd_error_malformed_archive);
-      goto release_raw_armap;
+      goto release_symdefs;
     }
 
   /* OK, build the carsyms.  */
@@ -1128,8 +1134,6 @@ do_slurp_coff_armap (bfd *abfd)
 
   return TRUE;
 
-release_raw_armap:
-  bfd_release (abfd, raw_armap);
 release_symdefs:
   bfd_release (abfd, (ardata)->symdefs);
   return FALSE;
@@ -1238,7 +1242,7 @@ _bfd_slurp_extended_name_table (bfd *abfd)
 	goto byebye;
 
       bfd_ardata (abfd)->extended_names_size = amt;
-      bfd_ardata (abfd)->extended_names = (char *) bfd_zalloc (abfd, amt + 1);
+      bfd_ardata (abfd)->extended_names = (char *) bfd_alloc (abfd, amt + 1);
       if (bfd_ardata (abfd)->extended_names == NULL)
 	{
 	byebye:
@@ -1256,6 +1260,7 @@ _bfd_slurp_extended_name_table (bfd *abfd)
 	  bfd_ardata (abfd)->extended_names = NULL;
 	  goto byebye;
 	}
+      bfd_ardata (abfd)->extended_names[amt] = 0;
 
       /* Since the archive is supposed to be printable if it contains
 	 text, the entries in the list are newline-padded, not null
@@ -1607,7 +1612,7 @@ _bfd_construct_extended_name_table (bfd *abfd,
   if (total_namelen == 0)
     return TRUE;
 
-  *tabloc = (char *) bfd_zalloc (abfd, total_namelen);
+  *tabloc = (char *) bfd_alloc (abfd, total_namelen);
   if (*tabloc == NULL)
     return FALSE;
 
@@ -1664,16 +1669,14 @@ _bfd_construct_extended_name_table (bfd *abfd,
 	    stroff = last_stroff;
 	  else
 	    {
-	      strcpy (strptr, normal);
-	      if (! trailing_slash)
-		strptr[thislen] = ARFMAG[1];
-	      else
-		{
-		  strptr[thislen] = '/';
-		  strptr[thislen + 1] = ARFMAG[1];
-		}
+	      last_filename = filename;
 	      stroff = strptr - *tabloc;
 	      last_stroff = stroff;
+	      memcpy (strptr, normal, thislen);
+	      strptr += thislen;
+	      if (trailing_slash)
+		*strptr++ = '/';
+	      *strptr++ = ARFMAG[1];
 	    }
 	  hdr->ar_name[0] = ar_padchar (current);
 	  if (bfd_is_thin_archive (abfd) && current->origin > 0)
@@ -1686,13 +1689,6 @@ _bfd_construct_extended_name_table (bfd *abfd,
 	    }
 	  else
 	    _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff);
-	  if (normal != last_filename)
-	    {
-	      strptr += thislen + 1;
-	      if (trailing_slash)
-		++strptr;
-	      last_filename = filename;
-	    }
 	}
     }



More information about the Binutils-cvs mailing list