arrfab / rpms / glibc

Forked from rpms/glibc 4 years ago
Clone

Blame SOURCES/glibc-rh1074410.patch

147e83
commit ae42bbc55a9e05976269026ddabcfb917f6e922f
147e83
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
147e83
Date:   Mon Mar 17 18:42:53 2014 +0530
147e83
147e83
    Change offset in fdopen only if setting O_APPEND
147e83
    
147e83
    fdopen should only be allowed to change the offset in the file it
147e83
    attaches to if it is setting O_APPEND.  If O_APPEND is already set, it
147e83
    should not change the state of the handle.
147e83
147e83
commit ea33158c96c53a64402a772186956c1f5cb556ae
147e83
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
147e83
Date:   Tue Mar 11 17:04:49 2014 +0530
147e83
147e83
    Fix offset caching for streams and use it for ftell (BZ #16680)
147e83
    
147e83
    The ftell implementation was made conservative to ensure that
147e83
    incorrectly cached offsets never affect it.  However, this causes
147e83
    problems for append mode when a file stream is rewound.  Additionally,
147e83
    the 'clever' trick of using stat to get position for append mode files
147e83
    caused more problems than it solved and broke old behavior.  I have
147e83
    described the various problems that it caused and then finally the
147e83
    solution.
147e83
    
147e83
    For a and a+ mode files, rewinding the stream should result in ftell
147e83
    returning 0 as the offset, but the stat() trick caused it to
147e83
    (incorrectly) always return the end of file.  Now I couldn't find
147e83
    anything in POSIX that specifies the stream position after rewind()
147e83
    for a file opened in 'a' mode, but for 'a+' mode it should be set to
147e83
    0.  For 'a' mode too, it probably makes sense to keep it set to 0 in
147e83
    the interest of retaining old behavior.
147e83
    
147e83
    The initial file position for append mode files is implementation
147e83
    defined, so the implementation could either retain the current file
147e83
    position or move the position to the end of file.  The earlier ftell
147e83
    implementation would move the offset to end of file for append-only
147e83
    mode, but retain the old offset for a+ mode.  It would also cache the
147e83
    offset (this detail is important).  My patch broke this and would set
147e83
    the initial position to end of file for both append modes, thus
147e83
    breaking old behavior.  I was ignorant enough to write an incorrect
147e83
    test case for it too.
147e83
    
147e83
    The Change:
147e83
    
147e83
    I have now brought back the behavior of seeking to end of file for
147e83
    append-only streams, but with a slight difference.  I don't cache the
147e83
    offset though, since we would want ftell to query the current file
147e83
    position through lseek while the stream is not active.  Since the
147e83
    offset is moved to the end of file, we can rely on the file position
147e83
    reported by lseek and we don't need to resort to the stat() nonsense.
147e83
    
147e83
    Finally, the cache is always reliable, except when there are unflished
147e83
    writes in an append mode stream (i.e. both a and a+).  In the latter
147e83
    case, it is safe to just do an lseek to SEEK_END.  The value can be
147e83
    safely cached too, since the file handle is already active at this
147e83
    point.  Incidentally, this is the only state change we affect in the
147e83
    file handle (apart from taking locks of course).
147e83
    
147e83
    I have also updated the test case to correct my impression of the
147e83
    initial file position for a+ streams to the initial behavior.  I have
147e83
    verified that this does not break any existing tests in the testsuite
147e83
    and also passes with the new tests.
147e83
147e83
commit b1dbb426e164ad1236c2c76268e03fec5c7a7bbe
147e83
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
147e83
Date:   Mon Mar 10 16:20:01 2014 +0530
147e83
147e83
    Fix up return codes for tests in tst-ftell-active-handler
147e83
    
147e83
    The test functions used a variable ret to store failure codes for
147e83
    individual tests, but the variable was incorrectly used to record
147e83
    other failure codes too, resulting in overwriting of the tests status.
147e83
    This is now fixed by making sure that the ret variable is used only
147e83
    for recording test failures.
147e83
    
147e83
    	* libio/tst-ftell-active-handler.c (do_ftell_test): Don't mix
147e83
    	up test status with function return status.
147e83
    	(do_write_test): Likewise.
147e83
    	(do_append_test): Likewise.
147e83
diff --git glibc-2.17-c758a686/libio/fileops.c glibc-2.17-c758a686/libio/fileops.c
147e83
index 2e7bc8d..cf68dbf 100644
147e83
--- glibc-2.17-c758a686/libio/fileops.c
147e83
+++ glibc-2.17-c758a686/libio/fileops.c
147e83
@@ -232,13 +232,18 @@ _IO_file_open (fp, filename, posix_mode, prot, read_write, is32not64)
147e83
     return NULL;
147e83
   fp->_fileno = fdesc;
147e83
   _IO_mask_flags (fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
147e83
-  if ((read_write & _IO_IS_APPENDING) && (read_write & _IO_NO_READS))
147e83
-    if (_IO_SEEKOFF (fp, (_IO_off64_t)0, _IO_seek_end, _IOS_INPUT|_IOS_OUTPUT)
147e83
-	== _IO_pos_BAD && errno != ESPIPE)
147e83
-      {
147e83
-	close_not_cancel (fdesc);
147e83
-	return NULL;
147e83
-      }
147e83
+  /* For append mode, send the file offset to the end of the file.  Don't
147e83
+     update the offset cache though, since the file handle is not active.  */
147e83
+  if ((read_write & (_IO_IS_APPENDING | _IO_NO_READS))
147e83
+      == (_IO_IS_APPENDING | _IO_NO_READS))
147e83
+    {
147e83
+      _IO_off64_t new_pos = _IO_SYSSEEK (fp, 0, _IO_seek_end);
147e83
+      if (new_pos == _IO_pos_BAD && errno != ESPIPE)
147e83
+	{
147e83
+	  close_not_cancel (fdesc);
147e83
+	  return NULL;
147e83
+	}
147e83
+    }
147e83
   _IO_link_in ((struct _IO_FILE_plus *) fp);
147e83
   return fp;
147e83
 }
147e83
@@ -929,43 +934,13 @@ _IO_file_sync_mmap (_IO_FILE *fp)
147e83
   return 0;
147e83
 }
147e83
 
147e83
-/* Get the current file offset using a system call.  This is the safest method
147e83
-   to get the current file offset, since we are sure that we get the current
147e83
-   state of the file.  Before the stream handle is activated (by using fread,
147e83
-   fwrite, etc.), an application may alter the state of the file descriptor
147e83
-   underlying it by calling read/write/lseek on it.  Using a cached offset at
147e83
-   this point will result in returning the incorrect value.  Same is the case
147e83
-   when one switches from reading in a+ mode to writing, where the buffer has
147e83
-   not been flushed - the cached offset would reflect the reading position
147e83
-   while the actual write position would be at the end of the file.
147e83
-
147e83
-   do_ftell and do_ftell_wide may resort to using the cached offset in some
147e83
-   special cases instead of calling get_file_offset, but those cases should be
147e83
-   thoroughly described.  */
147e83
-_IO_off64_t
147e83
-get_file_offset (_IO_FILE *fp)
147e83
-{
147e83
-  if ((fp->_flags & _IO_IS_APPENDING) == _IO_IS_APPENDING)
147e83
-    {
147e83
-      struct stat64 st;
147e83
-      bool ret = (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode));
147e83
-      if (ret)
147e83
-	return st.st_size;
147e83
-      else
147e83
-	return EOF;
147e83
-    }
147e83
-  else
147e83
-    return _IO_SYSSEEK (fp, 0, _IO_seek_cur);
147e83
-}
147e83
-
147e83
-
147e83
-/* ftell{,o} implementation.  Don't modify any state of the file pointer while
147e83
-   we try to get the current state of the stream.  */
147e83
+/* ftell{,o} implementation.  The only time we modify the state of the stream
147e83
+   is when we have unflushed writes.  In that case we seek to the end and
147e83
+   record that offset in the stream object.  */
147e83
 static _IO_off64_t
147e83
 do_ftell (_IO_FILE *fp)
147e83
 {
147e83
-  _IO_off64_t result = 0;
147e83
-  bool use_cached_offset = false;
147e83
+  _IO_off64_t result, offset = 0;
147e83
 
147e83
   /* No point looking at unflushed data if we haven't allocated buffers
147e83
      yet.  */
147e83
@@ -974,39 +949,37 @@ do_ftell (_IO_FILE *fp)
147e83
       bool was_writing = (fp->_IO_write_ptr > fp->_IO_write_base
147e83
 			  || _IO_in_put_mode (fp));
147e83
 
147e83
+      bool append_mode = (fp->_flags & _IO_IS_APPENDING) == _IO_IS_APPENDING;
147e83
+
147e83
+      /* When we have unflushed writes in append mode, seek to the end of the
147e83
+	 file and record that offset.  This is the only time we change the file
147e83
+	 stream state and it is safe since the file handle is active.  */
147e83
+      if (was_writing && append_mode)
147e83
+	{
147e83
+	  result = _IO_SYSSEEK (fp, 0, _IO_seek_end);
147e83
+	  if (result == _IO_pos_BAD)
147e83
+	    return EOF;
147e83
+	  else
147e83
+	    fp->_offset = result;
147e83
+	}
147e83
+
147e83
       /* Adjust for unflushed data.  */
147e83
       if (!was_writing)
147e83
-	result -= fp->_IO_read_end - fp->_IO_read_ptr;
147e83
+	offset -= fp->_IO_read_end - fp->_IO_read_ptr;
147e83
       else
147e83
-	result += fp->_IO_write_ptr - fp->_IO_read_end;
147e83
-
147e83
-      /* It is safe to use the cached offset when available if there is
147e83
-	 unbuffered data (indicating that the file handle is active) and the
147e83
-	 handle is not for a file open in a+ mode.  The latter condition is
147e83
-	 because there could be a scenario where there is a switch from read
147e83
-	 mode to write mode using an fseek to an arbitrary position.  In this
147e83
-	 case, there would be unbuffered data due to be appended to the end of
147e83
-	 the file, but the offset may not necessarily be the end of the
147e83
-	 file.  It is fine to use the cached offset when the a+ stream is in
147e83
-	 read mode though, since the offset is maintained correctly in that
147e83
-	 case.  Note that this is not a comprehensive set of cases when the
147e83
-	 offset is reliable.  The offset may be reliable even in some cases
147e83
-	 where there is no unflushed input and the handle is active, but it's
147e83
-	 just that we don't have a way to identify that condition reliably.  */
147e83
-      use_cached_offset = (result != 0 && fp->_offset != _IO_pos_BAD
147e83
-			   && ((fp->_flags & (_IO_IS_APPENDING | _IO_NO_READS))
147e83
-			       == (_IO_IS_APPENDING | _IO_NO_READS)
147e83
-			       && was_writing));
147e83
+	offset += fp->_IO_write_ptr - fp->_IO_read_end;
147e83
     }
147e83
 
147e83
-  if (use_cached_offset)
147e83
-    result += fp->_offset;
147e83
+  if (fp->_offset != _IO_pos_BAD)
147e83
+    result = fp->_offset;
147e83
   else
147e83
-    result += get_file_offset (fp);
147e83
+    result = _IO_SYSSEEK (fp, 0, _IO_seek_cur);
147e83
 
147e83
   if (result == EOF)
147e83
     return result;
147e83
 
147e83
+  result += offset;
147e83
+
147e83
   if (result < 0)
147e83
     {
147e83
       __set_errno (EINVAL);
147e83
@@ -1016,7 +989,6 @@ do_ftell (_IO_FILE *fp)
147e83
   return result;
147e83
 }
147e83
 
147e83
-
147e83
 _IO_off64_t
147e83
 _IO_new_file_seekoff (fp, offset, dir, mode)
147e83
      _IO_FILE *fp;
147e83
diff --git glibc-2.17-c758a686/libio/iofdopen.c glibc-2.17-c758a686/libio/iofdopen.c
147e83
index 3f266f7..b36d21d 100644
147e83
--- glibc-2.17-c758a686/libio/iofdopen.c
147e83
+++ glibc-2.17-c758a686/libio/iofdopen.c
147e83
@@ -59,6 +59,11 @@ _IO_new_fdopen (fd, mode)
147e83
   int i;
147e83
   int use_mmap = 0;
147e83
 
147e83
+  /* Decide whether we modify the offset of the file we attach to and seek to
147e83
+     the end of file.  We only do this if the mode is 'a' and if the file
147e83
+     descriptor did not have O_APPEND in its flags already.  */
147e83
+  bool do_seek = false;
147e83
+
147e83
   switch (*mode)
147e83
     {
147e83
     case 'r':
147e83
@@ -128,6 +133,7 @@ _IO_new_fdopen (fd, mode)
147e83
      */
147e83
   if ((posix_mode & O_APPEND) && !(fd_flags & O_APPEND))
147e83
     {
147e83
+      do_seek = true;
147e83
 #ifdef F_SETFL
147e83
       if (_IO_fcntl (fd, F_SETFL, fd_flags | O_APPEND) == -1)
147e83
 #endif
147e83
@@ -167,6 +173,16 @@ _IO_new_fdopen (fd, mode)
147e83
   _IO_mask_flags (&new_f->fp.file, read_write,
147e83
 		  _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
147e83
 
147e83
+  /* For append mode, set the file offset to the end of the file if we added
147e83
+     O_APPEND to the file descriptor flags.  Don't update the offset cache
147e83
+     though, since the file handle is not active.  */
147e83
+  if (do_seek && ((read_write & (_IO_IS_APPENDING | _IO_NO_READS))
147e83
+		  == (_IO_IS_APPENDING | _IO_NO_READS)))
147e83
+    {
147e83
+      _IO_off64_t new_pos = _IO_SYSSEEK (&new_f->fp.file, 0, _IO_seek_end);
147e83
+      if (new_pos == _IO_pos_BAD && errno != ESPIPE)
147e83
+	return NULL;
147e83
+    }
147e83
   return &new_f->fp.file;
147e83
 }
147e83
 libc_hidden_ver (_IO_new_fdopen, _IO_fdopen)
147e83
diff --git glibc-2.17-c758a686/libio/tst-ftell-active-handler.c glibc-2.17-c758a686/libio/tst-ftell-active-handler.c
147e83
index 54bfe63..e9dc7b3 100644
147e83
--- glibc-2.17-c758a686/libio/tst-ftell-active-handler.c
147e83
+++ glibc-2.17-c758a686/libio/tst-ftell-active-handler.c
147e83
@@ -88,6 +88,107 @@ static size_t file_len;
147e83
 typedef int (*fputs_func_t) (const void *data, FILE *fp);
147e83
 fputs_func_t fputs_func;
147e83
 
147e83
+/* Test that ftell output after a rewind is correct.  */
147e83
+static int
147e83
+do_rewind_test (const char *filename)
147e83
+{
147e83
+  int ret = 0;
147e83
+  struct test
147e83
+    {
147e83
+      const char *mode;
147e83
+      int fd_mode;
147e83
+      size_t old_off;
147e83
+      size_t new_off;
147e83
+    } test_modes[] = {
147e83
+	  {"w", O_WRONLY, 0, data_len},
147e83
+	  {"w+", O_RDWR, 0, data_len},
147e83
+	  {"r+", O_RDWR, 0, data_len},
147e83
+	  /* The new offsets for 'a' and 'a+' modes have to factor in the
147e83
+	     previous writes since they always append to the end of the
147e83
+	     file.  */
147e83
+	  {"a", O_WRONLY, 0, 3 * data_len},
147e83
+	  {"a+", O_RDWR, 0, 4 * data_len},
147e83
+    };
147e83
+
147e83
+  /* Empty the file before the test so that our offsets are simple to
147e83
+     calculate.  */
147e83
+  FILE *fp = fopen (filename, "w");
147e83
+  if (fp == NULL)
147e83
+    {
147e83
+      printf ("Failed to open file for emptying\n");
147e83
+      return 1;
147e83
+    }
147e83
+  fclose (fp);
147e83
+
147e83
+  for (int j = 0; j < 2; j++)
147e83
+    {
147e83
+      for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
147e83
+	{
147e83
+	  FILE *fp;
147e83
+	  int fd;
147e83
+	  int fileret;
147e83
+
147e83
+	  printf ("\trewind: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
147e83
+		  test_modes[i].mode);
147e83
+
147e83
+	  if (j == 0)
147e83
+	    fileret = get_handles_fdopen (filename, fd, fp,
147e83
+					  test_modes[i].fd_mode,
147e83
+					  test_modes[i].mode);
147e83
+	  else
147e83
+	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
147e83
+
147e83
+	  if (fileret != 0)
147e83
+	    return fileret;
147e83
+
147e83
+	  /* Write some content to the file, rewind and ensure that the ftell
147e83
+	     output after the rewind is 0.  POSIX does not specify what the
147e83
+	     behavior is when a file is rewound in 'a' mode, so we retain
147e83
+	     current behavior, which is to keep the 0 offset.  */
147e83
+	  size_t written = fputs_func (data, fp);
147e83
+
147e83
+	  if (written == EOF)
147e83
+	    {
147e83
+	      printf ("fputs[1] failed to write data\n");
147e83
+	      ret |= 1;
147e83
+	    }
147e83
+
147e83
+	  rewind (fp);
147e83
+	  long offset = ftell (fp);
147e83
+
147e83
+	  if (offset != test_modes[i].old_off)
147e83
+	    {
147e83
+	      printf ("Incorrect old offset.  Expected %zu, but got %ld, ",
147e83
+		      test_modes[i].old_off, offset);
147e83
+	      ret |= 1;
147e83
+	    }
147e83
+	  else
147e83
+	    printf ("old offset = %ld, ", offset);
147e83
+
147e83
+	  written = fputs_func (data, fp);
147e83
+
147e83
+	  if (written == EOF)
147e83
+	    {
147e83
+	      printf ("fputs[1] failed to write data\n");
147e83
+	      ret |= 1;
147e83
+	    }
147e83
+
147e83
+	  /* After this write, the offset in append modes should factor in the
147e83
+	     implicit lseek to the end of file.  */
147e83
+	  offset = ftell (fp);
147e83
+	  if (offset != test_modes[i].new_off)
147e83
+	    {
147e83
+	      printf ("Incorrect new offset.  Expected %zu, but got %ld\n",
147e83
+		      test_modes[i].new_off, offset);
147e83
+	      ret |= 1;
147e83
+	    }
147e83
+	  else
147e83
+	    printf ("new offset = %ld\n", offset);
147e83
+	}
147e83
+    }
147e83
+  return ret;
147e83
+}
147e83
+
147e83
 /* Test that the value of ftell is not cached when the stream handle is not
147e83
    active.  */
147e83
 static int
147e83
@@ -107,11 +208,13 @@ do_ftell_test (const char *filename)
147e83
 	  {"w", O_WRONLY, 0, data_len},
147e83
 	  {"w+", O_RDWR, 0, data_len},
147e83
 	  {"r+", O_RDWR, 0, data_len},
147e83
-	  /* For 'a' and 'a+' modes, the initial file position should be the
147e83
+	  /* For the 'a' mode, the initial file position should be the
147e83
 	     current end of file. After the write, the offset has data_len
147e83
-	     added to the old value.  */
147e83
+	     added to the old value.  For a+ mode however, the initial file
147e83
+	     position is the file position of the underlying file descriptor,
147e83
+	     since it is initially assumed to be in read mode.  */
147e83
 	  {"a", O_WRONLY, data_len, 2 * data_len},
147e83
-	  {"a+", O_RDWR, 2 * data_len, 3 * data_len},
147e83
+	  {"a+", O_RDWR, 0, 3 * data_len},
147e83
     };
147e83
   for (int j = 0; j < 2; j++)
147e83
     {
147e83
@@ -119,17 +222,20 @@ do_ftell_test (const char *filename)
147e83
 	{
147e83
 	  FILE *fp;
147e83
 	  int fd;
147e83
+	  int fileret;
147e83
+
147e83
 	  printf ("\tftell: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
147e83
 		  test_modes[i].mode);
147e83
 
147e83
 	  if (j == 0)
147e83
-	    ret = get_handles_fdopen (filename, fd, fp, test_modes[i].fd_mode,
147e83
-				      test_modes[i].mode);
147e83
+	    fileret = get_handles_fdopen (filename, fd, fp,
147e83
+					  test_modes[i].fd_mode,
147e83
+					  test_modes[i].mode);
147e83
 	  else
147e83
-	    ret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
147e83
+	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
147e83
 
147e83
-	  if (ret != 0)
147e83
-	    return ret;
147e83
+	  if (fileret != 0)
147e83
+	    return fileret;
147e83
 
147e83
 	  long off = ftell (fp);
147e83
 	  if (off != test_modes[i].old_off)
147e83
@@ -143,13 +249,18 @@ do_ftell_test (const char *filename)
147e83
 
147e83
 	  /* The effect of this write on the offset should be seen in the ftell
147e83
 	     call that follows it.  */
147e83
-	  int ret = write (fd, data, data_len);
147e83
+	  int write_ret = write (fd, data, data_len);
147e83
+	  if (write_ret != data_len)
147e83
+	    {
147e83
+	      printf ("write failed (%m)\n");
147e83
+	      ret |= 1;
147e83
+	    }
147e83
 	  off = ftell (fp);
147e83
 
147e83
 	  if (off != test_modes[i].new_off)
147e83
 	    {
147e83
 	      printf ("Incorrect new offset.  Expected %zu but got %ld\n",
147e83
-		      test_modes[i].old_off, off);
147e83
+		      test_modes[i].new_off, off);
147e83
 	      ret |= 1;
147e83
 	    }
147e83
 	  else
147e83
@@ -184,21 +295,23 @@ do_write_test (const char *filename)
147e83
     {
147e83
       for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
147e83
 	{
147e83
+	  int fileret;
147e83
 	  printf ("\twrite: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
147e83
 		  test_modes[i].mode);
147e83
 
147e83
 	  if (j == 0)
147e83
-	    ret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
147e83
+	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
147e83
 	  else
147e83
-	    ret = get_handles_fdopen (filename, fd, fp, test_modes[i].fd_mode,
147e83
-				      test_modes[i].mode);
147e83
+	    fileret = get_handles_fdopen (filename, fd, fp,
147e83
+					  test_modes[i].fd_mode,
147e83
+					  test_modes[i].mode);
147e83
 
147e83
-	  if (ret != 0)
147e83
-	    return ret;
147e83
+	  if (fileret != 0)
147e83
+	    return fileret;
147e83
 
147e83
 	  /* Move offset to just before the end of the file.  */
147e83
-	  off_t ret = lseek (fd, file_len - 1, SEEK_SET);
147e83
-	  if (ret == -1)
147e83
+	  off_t seek_ret = lseek (fd, file_len - 1, SEEK_SET);
147e83
+	  if (seek_ret == -1)
147e83
 	    {
147e83
 	      printf ("lseek failed: %m\n");
147e83
 	      ret |= 1;
147e83
@@ -258,17 +371,20 @@ do_append_test (const char *filename)
147e83
     {
147e83
       for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
147e83
 	{
147e83
+	  int fileret;
147e83
+
147e83
 	  printf ("\tappend: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
147e83
 		  test_modes[i].mode);
147e83
 
147e83
 	  if (j == 0)
147e83
-	    ret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
147e83
+	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
147e83
 	  else
147e83
-	    ret = get_handles_fdopen (filename, fd, fp, test_modes[i].fd_mode,
147e83
-				      test_modes[i].mode);
147e83
+	    fileret = get_handles_fdopen (filename, fd, fp,
147e83
+					  test_modes[i].fd_mode,
147e83
+					  test_modes[i].mode);
147e83
 
147e83
-	  if (ret != 0)
147e83
-	    return ret;
147e83
+	  if (fileret != 0)
147e83
+	    return fileret;
147e83
 
147e83
 	  /* Write some data.  */
147e83
 	  size_t written = fputs_func (data, fp);
147e83
@@ -298,6 +414,61 @@ do_append_test (const char *filename)
147e83
 	}
147e83
     }
147e83
 
147e83
+  /* For fdopen in 'a' mode, the file descriptor should not change if the file
147e83
+     is already open with the O_APPEND flag set.  */
147e83
+  fd = open (filename, O_WRONLY | O_APPEND, 0);
147e83
+  if (fd == -1)
147e83
+    {
147e83
+      printf ("open(O_APPEND) failed: %m\n");
147e83
+      return 1;
147e83
+    }
147e83
+
147e83
+  off_t seek_ret = lseek (fd, file_len - 1, SEEK_SET);
147e83
+  if (seek_ret == -1)
147e83
+    {
147e83
+      printf ("lseek[O_APPEND][0] failed: %m\n");
147e83
+      ret |= 1;
147e83
+    }
147e83
+
147e83
+  fp = fdopen (fd, "a");
147e83
+  if (fp == NULL)
147e83
+    {
147e83
+      printf ("fdopen(O_APPEND) failed: %m\n");
147e83
+      close (fd);
147e83
+      return 1;
147e83
+    }
147e83
+
147e83
+  off_t new_seek_ret = lseek (fd, 0, SEEK_CUR);
147e83
+  if (seek_ret == -1)
147e83
+    {
147e83
+      printf ("lseek[O_APPEND][1] failed: %m\n");
147e83
+      ret |= 1;
147e83
+    }
147e83
+
147e83
+  printf ("\tappend: fdopen (file, \"a\"): O_APPEND: ");
147e83
+
147e83
+  if (seek_ret != new_seek_ret)
147e83
+    {
147e83
+      printf ("incorrectly modified file offset to %ld, should be %ld",
147e83
+	      new_seek_ret, seek_ret);
147e83
+      ret |= 1;
147e83
+    }
147e83
+  else
147e83
+    printf ("retained current file offset %ld", seek_ret);
147e83
+
147e83
+  new_seek_ret = ftello (fp);
147e83
+
147e83
+  if (seek_ret != new_seek_ret)
147e83
+    {
147e83
+      printf (", ftello reported incorrect offset %ld, should be %ld\n",
147e83
+	      new_seek_ret, seek_ret);
147e83
+      ret |= 1;
147e83
+    }
147e83
+  else
147e83
+    printf (", ftello reported correct offset %ld\n", seek_ret);
147e83
+
147e83
+  fclose (fp);
147e83
+
147e83
   return ret;
147e83
 }
147e83
 
147e83
@@ -309,6 +480,7 @@ do_one_test (const char *filename)
147e83
   ret |= do_ftell_test (filename);
147e83
   ret |= do_write_test (filename);
147e83
   ret |= do_append_test (filename);
147e83
+  ret |= do_rewind_test (filename);
147e83
 
147e83
   return ret;
147e83
 }
147e83
diff --git glibc-2.17-c758a686/libio/wfileops.c glibc-2.17-c758a686/libio/wfileops.c
147e83
index 8b2e108..3199861 100644
147e83
--- glibc-2.17-c758a686/libio/wfileops.c
147e83
+++ glibc-2.17-c758a686/libio/wfileops.c
147e83
@@ -597,12 +597,12 @@ done:
147e83
 }
147e83
 
147e83
 /* ftell{,o} implementation for wide mode.  Don't modify any state of the file
147e83
-   pointer while we try to get the current state of the stream.  */
147e83
+   pointer while we try to get the current state of the stream except in one
147e83
+   case, which is when we have unflushed writes in append mode.  */
147e83
 static _IO_off64_t
147e83
 do_ftell_wide (_IO_FILE *fp)
147e83
 {
147e83
   _IO_off64_t result, offset = 0;
147e83
-  bool use_cached_offset = false;
147e83
 
147e83
   /* No point looking for offsets in the buffer if it hasn't even been
147e83
      allocated.  */
147e83
@@ -615,6 +615,20 @@ do_ftell_wide (_IO_FILE *fp)
147e83
 			   > fp->_wide_data->_IO_write_base)
147e83
 			  || _IO_in_put_mode (fp));
147e83
 
147e83
+      bool append_mode = (fp->_flags & _IO_IS_APPENDING) == _IO_IS_APPENDING;
147e83
+
147e83
+      /* When we have unflushed writes in append mode, seek to the end of the
147e83
+	 file and record that offset.  This is the only time we change the file
147e83
+	 stream state and it is safe since the file handle is active.  */
147e83
+      if (was_writing && append_mode)
147e83
+	{
147e83
+	  result = _IO_SYSSEEK (fp, 0, _IO_seek_end);
147e83
+	  if (result == _IO_pos_BAD)
147e83
+	    return EOF;
147e83
+	  else
147e83
+	    fp->_offset = result;
147e83
+	}
147e83
+
147e83
       /* XXX For wide stream with backup store it is not very
147e83
 	 reasonable to determine the offset.  The pushed-back
147e83
 	 character might require a state change and we need not be
147e83
@@ -703,37 +717,24 @@ do_ftell_wide (_IO_FILE *fp)
147e83
 	     position is fp._offset - (_IO_read_end - new_write_ptr).  */
147e83
 	  offset -= fp->_IO_read_end - fp->_IO_write_ptr;
147e83
 	}
147e83
-
147e83
-      /* It is safe to use the cached offset when available if there is
147e83
-	 unbuffered data (indicating that the file handle is active) and
147e83
-	 the handle is not for a file open in a+ mode.  The latter
147e83
-	 condition is because there could be a scenario where there is a
147e83
-	 switch from read mode to write mode using an fseek to an arbitrary
147e83
-	 position.  In this case, there would be unbuffered data due to be
147e83
-	 appended to the end of the file, but the offset may not
147e83
-	 necessarily be the end of the file.  It is fine to use the cached
147e83
-	 offset when the a+ stream is in read mode though, since the offset
147e83
-	 is maintained correctly in that case.  Note that this is not a
147e83
-	 comprehensive set of cases when the offset is reliable.  The
147e83
-	 offset may be reliable even in some cases where there is no
147e83
-	 unflushed input and the handle is active, but it's just that we
147e83
-	 don't have a way to identify that condition reliably.  */
147e83
-      use_cached_offset = (offset != 0 && fp->_offset != _IO_pos_BAD
147e83
-			   && ((fp->_flags & (_IO_IS_APPENDING | _IO_NO_READS))
147e83
-			       == (_IO_IS_APPENDING | _IO_NO_READS)
147e83
-			       && was_writing));
147e83
     }
147e83
 
147e83
-  if (use_cached_offset)
147e83
+  if (fp->_offset != _IO_pos_BAD)
147e83
     result = fp->_offset;
147e83
   else
147e83
-    result = get_file_offset (fp);
147e83
+    result = _IO_SYSSEEK (fp, 0, _IO_seek_cur);
147e83
 
147e83
   if (result == EOF)
147e83
     return result;
147e83
 
147e83
   result += offset;
147e83
 
147e83
+  if (result < 0)
147e83
+    {
147e83
+      __set_errno (EINVAL);
147e83
+      return EOF;
147e83
+    }
147e83
+
147e83
   return result;
147e83
 }
147e83