]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
alpha-vms: large memory allocation
authorAlan Modra <amodra@gmail.com>
Fri, 28 Feb 2020 02:05:44 +0000 (12:35 +1030)
committerAlan Modra <amodra@gmail.com>
Fri, 28 Feb 2020 02:56:30 +0000 (13:26 +1030)
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.

bfd/ChangeLog
bfd/vms-alpha.c

index b300326b0fccea981d9736a9e27a3e8b511c20ad..57e6b7c923979645aa0f5497804eada233a53ea2 100644 (file)
@@ -1,3 +1,8 @@
+2020-02-28  Alan Modra  <amodra@gmail.com>
+
+       * vms-alpha.c (alpha_vms_object_p): Use _bfd_malloc_and_read.
+       Remove duplicate undersize check.
+
 2020-02-27  Alan Modra  <amodra@gmail.com>
 
        PR 24511
index 5be475a53f30cc3521f1f4913cbbf02db0475d36..8470047498ec335d15f7a855844d01bad7d67ebe 100644 (file)
@@ -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))