]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
__libelf_readall: Fetch file size if not yet known.
authorRoland McGrath <roland@redhat.com>
Tue, 10 Nov 2009 21:34:09 +0000 (13:34 -0800)
committerRoland McGrath <roland@redhat.com>
Tue, 10 Nov 2009 21:34:09 +0000 (13:34 -0800)
libelf/ChangeLog
libelf/elf_readall.c

index 974afa1572e72d8828fb4338e53aa76b1abc370b..8098f4e95a810a295701995da2d583db2297ff03 100644 (file)
@@ -1,3 +1,7 @@
+2009-11-10  Roland McGrath  <roland@redhat.com>
+
+       * elf_readall.c (__libelf_readall): Fetch file size if not yet known.
+
 2009-11-06  Mark Wielaard  <mjw@redhat.com>
 
        * elf_next.c (elf_next): Mark the archive header as unusable when
index 8f171b21c494a6da672f78b71a918c90ba064757..1f59932f1e3e438cdc3b1f42af724ee7149a14e4 100644 (file)
@@ -1,5 +1,5 @@
 /* Read all of the file associated with the descriptor.
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005 Red Hat, Inc.
+   Copyright (C) 1998-2009 Red Hat, Inc.
    This file is part of Red Hat elfutils.
    Contributed by Ulrich Drepper <drepper@redhat.com>, 1998.
 
@@ -54,6 +54,7 @@
 
 #include <errno.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include <system.h>
 #include "libelfP.h"
@@ -102,12 +103,30 @@ __libelf_readall (elf)
   /* If the file is not mmap'ed and not previously loaded, do it now.  */
   if (elf->map_address == NULL)
     {
-      char *mem;
+      char *mem = NULL;
 
       /* If this is an archive and we have derived descriptors get the
         locks for all of them.  */
       libelf_acquire_all (elf);
 
+      if (elf->maximum_size == ~((size_t) 0))
+       {
+         /* We don't yet know how large the file is.   Determine that now.  */
+         struct stat st;
+
+         if (fstat (elf->fildes, &st) < 0)
+           goto read_error;
+
+         if (sizeof (size_t) >= sizeof (st.st_size)
+             || st.st_size <= ~((size_t) 0))
+           elf->maximum_size = (size_t) st.st_size;
+         else
+           {
+             errno = EOVERFLOW;
+             goto read_error;
+           }
+       }
+
       /* Allocate all the memory we need.  */
       mem = (char *) malloc (elf->maximum_size);
       if (mem != NULL)
@@ -119,6 +138,7 @@ __libelf_readall (elf)
                        != elf->maximum_size))
            {
              /* Something went wrong.  */
+           read_error:
              __libelf_seterrno (ELF_E_READ_ERROR);
              free (mem);
            }