This is the mail archive of the binutils@sourceware.cygnus.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Fix bfd_read to cope with bad BIMs


Hi Ian

: I think bfd_seek should return an error on an attempt to seek past
: the end of the file, rather than silently truncating abfd->where. 

No sooner said than done:

Index: libbfd.c
===================================================================
RCS file: /cvs/binutils/binutils/bfd/libbfd.c,v
retrieving revision 1.7
diff -p -r1.7 libbfd.c
*** libbfd.c	1999/11/09 19:13:21	1.7
--- libbfd.c	2000/01/21 20:18:24
*************** bfd_read (ptr, size, nitems, abfd)
*** 274,280 ****
        get = size * nitems;
        if (abfd->where + get > bim->size)
  	{
! 	  get = bim->size - abfd->where;
  	  bfd_set_error (bfd_error_file_truncated);
  	}
        memcpy (ptr, bim->buffer + abfd->where, get);
--- 274,283 ----
        get = size * nitems;
        if (abfd->where + get > bim->size)
  	{
! 	  if (bim->size < abfd->where)
! 	    get = 0;
! 	  else
! 	    get = bim->size - abfd->where;
  	  bfd_set_error (bfd_error_file_truncated);
  	}
        memcpy (ptr, bim->buffer + abfd->where, get);
*************** bfd_seek (abfd, position, direction)
*** 677,686 ****
--- 680,700 ----
  
    if ((abfd->flags & BFD_IN_MEMORY) != 0)
      {
+       struct bfd_in_memory *bim;
+ 
+       bim = (struct bfd_in_memory *) abfd->iostream;
+       
        if (direction == SEEK_SET)
  	abfd->where = position;
        else
  	abfd->where += position;
+       
+       if (abfd->where > bim->size)
+ 	{
+ 	  abfd->where = bim->size;
+ 	  bfd_set_error (bfd_error_file_truncated);
+ 	}
+       
        return 0;
      }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]