This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [committed, PATCH] Check file size before getting section contents
On Mon, Jun 26, 2017 at 04:27:27PM -0700, H.J. Lu wrote:
> On Mon, Jun 26, 2017 at 4:15 PM, Alan Modra <amodra@gmail.com> wrote:
> > On Mon, Jun 26, 2017 at 03:49:12PM -0700, H.J. Lu wrote:
> >> filesz = bfd_get_file_size (abfd);
> >> + if (filesz < 0)
> >> {
> >> /* This should never happen. */
> >> abort ();
> >
> > This will abort for 2G files on some host/target combinations. Why is
> > that correct?
> >
>
> That is true. The problem is
>
> file_ptr
> bfd_get_size (bfd *abfd)
> {
> struct stat buf;
>
> if (abfd->iovec == NULL)
> return 0;
>
> if (abfd->iovec->bstat (abfd, &buf) != 0)
> return 0;
>
> return buf.st_size;
> }
>
> Why isn't it "ufile_ptr".
I'm not sure of the history. However, the question of bfd_get_size
return type being signed isn't that relevant. What matters more is
the type used in the functions you patched, and that should be
unsigned, and the aborts removed. Also,
_bfd_generic_get_section_contents_in_window has the same problem that
Pedro fixed for _bfd_generic_get_section_contents.
PR binutils/21665
* libbfd.c (_bfd_generic_get_section_contents): Delete abort.
Use unsigned file pointer type, and remove cast.
* libbfd.c (_bfd_generic_get_section_contents_in_window): Likewise.
Add "count", not "sz".
diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index b8c65b5..0776451 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -789,7 +789,7 @@ _bfd_generic_get_section_contents (bfd *abfd,
bfd_size_type count)
{
bfd_size_type sz;
- file_ptr filesz;
+ ufile_ptr filesz;
if (count == 0)
return TRUE;
@@ -813,14 +813,9 @@ _bfd_generic_get_section_contents (bfd *abfd,
else
sz = section->size;
filesz = bfd_get_file_size (abfd);
- if (filesz < 0)
- {
- /* This should never happen. */
- abort ();
- }
if (offset + count < count
|| offset + count > sz
- || (section->filepos + offset + count) > (bfd_size_type) filesz)
+ || section->filepos + offset + count > filesz)
{
bfd_set_error (bfd_error_invalid_operation);
return FALSE;
@@ -843,7 +838,7 @@ _bfd_generic_get_section_contents_in_window
{
#ifdef USE_MMAP
bfd_size_type sz;
- file_ptr filesz;
+ ufile_ptr filesz;
if (count == 0)
return TRUE;
@@ -877,13 +872,8 @@ _bfd_generic_get_section_contents_in_window
else
sz = section->size;
filesz = bfd_get_file_size (abfd);
- if (filesz < 0)
- {
- /* This should never happen. */
- abort ();
- }
if (offset + count > sz
- || (section->filepos + offset + sz) > (bfd_size_type) filesz
+ || section->filepos + offset + count > filesz
|| ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
TRUE))
return FALSE;
--
Alan Modra
Australia Development Lab, IBM