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