This is the mail archive of the binutils@sources.redhat.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]

Clear new space in fseek extended patches


Hi Guys,

  I am planning to apply the patch below unless there are any
  objections.  It adds a new host specific define which specifies
  whether the host zeroes newly acquired file space obtained by
  fseek()ing past the end of the file.  If the host does not do this,
  then the patch extends the bfd_seek function to do it instead.

  In theory this could be determined by autoconf, (at least for native
  builds) but there is no guarantee on a host that does not zero the
  file space that zeroes will not already be present in the new
  blocks.

Cheers
	Nick

2000-08-30  Nick Clifton  <nickc@cygnus.com> & Fred Fish  <fnf@be.com>.

	* configure.host (*-*-beos): New entry, define
        CLEAR_FSEEK_SPACE.
	(*-*-windows): Add CLEAR_FSEEK_SPACE.

	* libbfd.c (bfd_seek): If CLEAR_FSEEK_SPACE is defined write
	zeros into extended file space.

Index: bfd/configure.host
===================================================================
RCS file: /cvs/src//src/bfd/configure.host,v
retrieving revision 1.4
diff -p -r1.4 configure.host
*** configure.host	2000/07/25 19:29:42	1.4
--- configure.host	2000/08/30 23:58:58
*************** m68*-hp-hpux*)		HDEFINES=-DHOST_HP300HPU
*** 52,62 ****
--- 52,65 ----
  			HOST_U_64BIT_TYPE="unsigned long long"
  			;;
  
+ *-*-beos*)		HDEFINES=-DCLEAR_FSEEK_SPACE ;;
+ 
  *-*-solaris*)		HOST_64BIT_TYPE="long long"
  			HOST_U_64BIT_TYPE="unsigned long long"
  			;;
  
  *-*-windows*)
+ 			HDEFINES=-DCLEAR_FSEEK_SPACE ;;
  			HOST_64BIT_TYPE=__int64
  			HOST_U_64BIT_TYPE="unsigned __int64"
  # The following krock is necessary because we can't run the build compiler

Index: bfd/libbfd.c
===================================================================
RCS file: /cvs/src//src/bfd/libbfd.c,v
retrieving revision 1.12
diff -p -r1.12 libbfd.c
*** libbfd.c	2000/07/31 18:49:56	1.12
--- libbfd.c	2000/08/30 23:58:58
*************** bfd_seek (abfd, position, direction)
*** 758,763 ****
--- 758,810 ----
    if (direction == SEEK_SET && abfd->my_archive != NULL)
      file_position += abfd->origin;
  
+ #if defined (CLEAR_FSEEK_SPACE)
+   {
+     file_ptr eof;
+     file_ptr pos;
+ 
+     /* When extending a file beyond its original length the fseek()
+        function might not initialise the new space to zero.  This
+        means that random garbage can be picked up, which in turn means
+        that two identical files assembled and linked in the same way
+        can nevertheless still fail a binary compare.  We fix this here
+        by explicitly initialising the extra space, but only if the file
+        was opened for writing.  */
+     pos = ftell (f);
+     fseek (f, 0L, SEEK_END);
+     eof = ftell (f);
+ 
+     if (direction == SEEK_CUR)
+       {
+ 	direction = SEEK_SET;
+ 	file_position += pos;
+       }
+ 
+     if (eof < file_position && bfd_write_p (abfd))
+       {
+ 	file_ptr diff;
+ 	static char zeros[512];
+       
+ 	diff = file_position - eof;
+ 
+ 	while (diff >= sizeof (zeros))
+ 	  {
+ 	    fwrite (zeros, sizeof (zeros), 1, f);
+ 	    diff -= sizeof (zeros);
+ 	  }
+ 	
+ 	if (diff > 0)
+ 	  fwrite (zeros, diff, 1, f);
+ 	
+ 	/* In theory we do not need to perform the fseek now, since the fwrite
+ 	   calls will have moved the file pointer to the correct location.  In
+ 	   practice however we leave the call in, just in case something went
+ 	   wrong with the fwrites and we missed it.  (After all we are not
+ 	   checking their return codes).  */
+       }
+   }
+ #endif
+ 
    result = fseek (f, file_position, direction);
  
    if (result != 0)

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