olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1074410.patch

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