about summary refs log tree commit diff
path: root/libio/tst-ftell-active-handler.c
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@redhat.com>2014-12-04 08:08:37 +0530
committerSiddhesh Poyarekar <siddhesh@redhat.com>2014-12-04 08:08:37 +0530
commitbe349d7042de84c3c5157a5c1fbcad580aed33e1 (patch)
treee49154ef02c2b02573693a3af1a773d5ea3f2470 /libio/tst-ftell-active-handler.c
parente3d6dba5dfe2e125b15ea1dd36c8dfa373bb4956 (diff)
downloadglibc-be349d7042de84c3c5157a5c1fbcad580aed33e1.tar.gz
glibc-be349d7042de84c3c5157a5c1fbcad580aed33e1.tar.xz
glibc-be349d7042de84c3c5157a5c1fbcad580aed33e1.zip
ftell: seek to end only when there are unflushed bytes (BZ #17647)
Currently we seek to end of file if there are unflushed writes or the
stream is in write mode, to get the current offset for writing in
append mode, which is the end of file.  The latter case (i.e. stream
is in write mode, but no unflushed writes) is unnecessary since it
will only happen when the stream has just been flushed, in which case
the recorded offset ought to be reliable.

Removing that case lets ftell give the correct offset when it follows
an ftruncate.  The latter truncates the file, but does not change the
file position, due to which it is permissible to call ftell without an
intervening fseek call.

Tested on x86_64 to verify that the added test case fails without the
patch and succeeds with it, and that there are no additional
regressions due to it.

	[BZ #17647]
	* libio/fileops.c (do_ftell): Seek only when there are
	unflushed writes.
	* libio/wfileops.c (do_ftell_wide): Likewise.
	* libio/tst-ftell-active-handler.c (do_ftruncate_test): New
	test case.
	(do_one_test): Call it.
Diffstat (limited to 'libio/tst-ftell-active-handler.c')
-rw-r--r--libio/tst-ftell-active-handler.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/libio/tst-ftell-active-handler.c b/libio/tst-ftell-active-handler.c
index e9dc7b3d52..9f23c55072 100644
--- a/libio/tst-ftell-active-handler.c
+++ b/libio/tst-ftell-active-handler.c
@@ -88,6 +88,95 @@ static size_t file_len;
 typedef int (*fputs_func_t) (const void *data, FILE *fp);
 fputs_func_t fputs_func;
 
+/* This test verifies that the offset reported by ftell is correct after the
+   file is truncated using ftruncate.  ftruncate does not change the file
+   offset on truncation and hence, SEEK_CUR should continue to point to the
+   old offset and not be changed to the new offset.  */
+static int
+do_ftruncate_test (const char *filename)
+{
+  FILE *fp = NULL;
+  int fd;
+  int ret = 0;
+  struct test
+    {
+      const char *mode;
+      int fd_mode;
+    } test_modes[] = {
+	  {"r+", O_RDWR},
+	  {"w", O_WRONLY},
+	  {"w+", O_RDWR},
+	  {"a", O_WRONLY},
+	  {"a+", O_RDWR}
+    };
+
+  for (int j = 0; j < 2; j++)
+    {
+      for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
+	{
+	  int fileret;
+	  printf ("\tftruncate: %s (file, \"%s\"): ",
+		  j == 0 ? "fopen" : "fdopen",
+		  test_modes[i].mode);
+
+	  if (j == 0)
+	    fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
+	  else
+	    fileret = get_handles_fdopen (filename, fd, fp,
+					  test_modes[i].fd_mode,
+					  test_modes[i].mode);
+
+	  if (fileret != 0)
+	    return fileret;
+
+	  /* Write some data.  */
+	  size_t written = fputs_func (data, fp);
+
+	  if (written == EOF)
+	    {
+	      printf ("fputs[1] failed to write data\n");
+	      ret |= 1;
+	    }
+
+	  /* Record the offset.  */
+	  long offset = ftell (fp);
+
+	  /* Flush data to allow switching active handles.  */
+	  if (fflush (fp))
+	    {
+	      printf ("Flush failed: %m\n");
+	      ret |= 1;
+	    }
+
+	  /* Now truncate the file.  */
+	  if (ftruncate (fd, 0) != 0)
+	    {
+	      printf ("Failed to truncate file: %m\n");
+	      ret |= 1;
+	    }
+
+	  /* ftruncate does not change the offset, so there is no need to call
+	     anything to be able to switch active handles.  */
+	  long new_offset = ftell (fp);
+
+	  /* The offset should remain unchanged since ftruncate does not update
+	     it.  */
+	  if (offset != new_offset)
+	    {
+	      printf ("Incorrect offset.  Expected %zu, but got %ld\n",
+		      offset, new_offset);
+
+	      ret |= 1;
+	    }
+	  else
+	    printf ("offset = %ld\n", offset);
+
+	  fclose (fp);
+	}
+    }
+
+  return ret;
+}
 /* Test that ftell output after a rewind is correct.  */
 static int
 do_rewind_test (const char *filename)
@@ -481,6 +570,7 @@ do_one_test (const char *filename)
   ret |= do_write_test (filename);
   ret |= do_append_test (filename);
   ret |= do_rewind_test (filename);
+  ret |= do_ftruncate_test (filename);
 
   return ret;
 }