[PATCH] Fix mmap stdio fseek SEEK_END bug

Jakub Jelinek jakub@redhat.com
Thu Feb 21 05:39:00 GMT 2002


Hi!

Immediately after fopen, _IO_read_end == _IO_read_base == _IO_buf_base.
So, fseek* (..., SEEK_END) was acting like SEEK_SET, not SEEK_END.

2002-02-21  Jakub Jelinek  <jakub@redhat.com>

	* libio/fileops.c (_IO_file_seekoff_mmap): Fix fseek SEEK_END.
	* stdio-common/tst-fseek.c (main): Add test for this.

--- libc/libio/fileops.c.jj	Thu Feb 21 12:42:10 2002
+++ libc/libio/fileops.c	Thu Feb 21 14:41:22 2002
@@ -880,7 +880,7 @@ _IO_file_seekoff_mmap (fp, offset, dir, 
     case _IO_seek_set:
       break;
     case _IO_seek_end:
-      offset = fp->_IO_read_end - fp->_IO_read_base + offset;
+      offset += fp->_IO_buf_end - fp->_IO_buf_base;
       break;
     }
   /* At this point, dir==_IO_seek_set. */
--- libc/stdio-common/tst-fseek.c.jj	Thu Aug 23 18:49:10 2001
+++ libc/stdio-common/tst-fseek.c	Thu Feb 21 13:42:48 2002
@@ -419,6 +419,35 @@ main (void)
       result = 1;
     }
 
+  fclose (fp);
+
+#ifdef USE_IN_LIBIO
+  fp = fopen (fname, "r");
+  if (fp == NULL)
+    {
+      puts ("fopen() failed\n");
+      result = 1;
+    }
+  else if (fstat64 (fileno (fp), &st1) < 0)
+    {
+      puts ("fstat64() before fseeko() failed\n");
+      result = 1;
+    }
+  else if (fseeko (fp, 0, SEEK_END) != 0)
+    {
+      puts ("fseeko(fp, 0, SEEK_END) failed");
+      result = 1;
+    }
+  else if (ftello (fp) != st1.st_size)
+    {
+      printf ("fstat64 st_size %zd ftello %zd\n", st1.st_size,
+	      ftello (fp));
+      result = 1;
+    }
+  if (fp != NULL)
+    fclose (fp);
+#endif
+  
  out:
   unlink (fname);
 

	Jakub



More information about the Libc-hacker mailing list