alpha-vms: large memory allocation

Message ID 20200228025644.GJ32593@bubble.grove.modra.org
State New
Headers show
Series
  • alpha-vms: large memory allocation
Related show

Commit Message

Alan Modra Feb. 28, 2020, 2:56 a.m.
This patch simplifies reading of image headers.  It's really not worth
trying to avoid re-reading a 12 byte buffer and then read in
VMS_BLOCK_SIZE chunks, better just to throw the buffer away and use
_bfd_malloc_and_read which does checks against file size.

	* vms-alpha.c (alpha_vms_object_p): Use _bfd_malloc_and_read.
	Remove duplicate undersize check.


-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/bfd/vms-alpha.c b/bfd/vms-alpha.c
index 5be475a53f..8470047498 100644
--- a/bfd/vms-alpha.c
+++ b/bfd/vms-alpha.c
@@ -2719,7 +2719,7 @@  alpha_vms_object_p (bfd *abfd)
     }
 
   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET))
-    goto err_wrong_format;
+    goto error_ret;
 
   /* The first challenge with VMS is to discover the kind of the file.
 
@@ -2738,27 +2738,17 @@  alpha_vms_object_p (bfd *abfd)
      2 bytes size repeated) and 12 bytes for images (4 bytes major id,
      4 bytes minor id, 4 bytes length).  */
   test_len = 12;
-
-  /* Size the main buffer.  */
-  buf = (unsigned char *) bfd_malloc (test_len);
+  buf = _bfd_malloc_and_read (abfd, test_len, test_len);
   if (buf == NULL)
     goto error_ret;
   PRIV (recrd.buf) = buf;
   PRIV (recrd.buf_size) = test_len;
-
-  /* Initialize the record pointer.  */
   PRIV (recrd.rec) = buf;
 
-  if (bfd_bread (buf, test_len, abfd) != test_len)
-    goto err_wrong_format;
-
   /* Is it an image?  */
   if ((bfd_getl32 (buf) == EIHD__K_MAJORID)
       && (bfd_getl32 (buf + 4) == EIHD__K_MINORID))
     {
-      unsigned int to_read;
-      unsigned int read_so_far;
-      unsigned int remaining;
       unsigned int eisd_offset, eihs_offset;
 
       /* Extract the header size.  */
@@ -2768,44 +2758,25 @@  alpha_vms_object_p (bfd *abfd)
       if (PRIV (recrd.rec_size) == 0)
 	PRIV (recrd.rec_size) = sizeof (struct vms_eihd);
 
-      if (PRIV (recrd.rec_size) > PRIV (recrd.buf_size))
-	{
-	  buf = bfd_realloc_or_free (buf, PRIV (recrd.rec_size));
-
-	  if (buf == NULL)
-	    {
-	      PRIV (recrd.buf) = NULL;
-	      goto error_ret;
-	    }
-	  PRIV (recrd.buf) = buf;
-	  PRIV (recrd.buf_size) = PRIV (recrd.rec_size);
-	}
-
       /* PR 21813: Check for a truncated record.  */
-      if (PRIV (recrd.rec_size < test_len))
-	goto error_ret;
-      /* Read the remaining record.  */
-      remaining = PRIV (recrd.rec_size) - test_len;
-      to_read = MIN (VMS_BLOCK_SIZE - test_len, remaining);
-      read_so_far = test_len;
-
-      while (remaining > 0)
-	{
-	  if (bfd_bread (buf + read_so_far, to_read, abfd) != to_read)
-	    goto err_wrong_format;
+      /* PR 17512: file: 7d7c57c2.  */
+      if (PRIV (recrd.rec_size) < sizeof (struct vms_eihd))
+	goto err_wrong_format;
 
-	  read_so_far += to_read;
-	  remaining -= to_read;
+      if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET))
+	goto error_ret;
 
-	  to_read = MIN (VMS_BLOCK_SIZE, remaining);
-	}
+      free (PRIV (recrd.buf));
+      PRIV (recrd.buf) = NULL;
+      buf = _bfd_malloc_and_read (abfd, PRIV (recrd.rec_size),
+				  PRIV (recrd.rec_size));
+      if (buf == NULL)
+	goto error_ret;
 
-      /* Reset the record pointer.  */
+      PRIV (recrd.buf) = buf;
+      PRIV (recrd.buf_size) = PRIV (recrd.rec_size);
       PRIV (recrd.rec) = buf;
 
-      /* PR 17512: file: 7d7c57c2.  */
-      if (PRIV (recrd.rec_size) < sizeof (struct vms_eihd))
-	goto error_ret;
       vms_debug2 ((2, "file type is image\n"));
 
       if (!_bfd_vms_slurp_eihd (abfd, &eisd_offset, &eihs_offset))