From 1b088c829ee812003d7601c7cf458dd394598719 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Fri, 28 Feb 2020 12:35:44 +1030 Subject: [PATCH] alpha-vms: large memory allocation 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 | 5 +++++ bfd/vms-alpha.c | 59 +++++++++++++------------------------------------ 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/bfd/ChangeLog b/bfd/ChangeLog index b300326b0f..57e6b7c923 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,8 @@ +2020-02-28 Alan Modra + + * vms-alpha.c (alpha_vms_object_p): Use _bfd_malloc_and_read. + Remove duplicate undersize check. + 2020-02-27 Alan Modra PR 24511 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)) -- 2.34.1