about summary refs log tree commit diff
path: root/REORG.TODO/libio/bug-memstream1.c
diff options
context:
space:
mode:
authorZack Weinberg <zackw@panix.com>2017-06-08 15:39:03 -0400
committerZack Weinberg <zackw@panix.com>2017-06-08 15:39:03 -0400
commit5046dbb4a7eba5eccfd258f92f4735c9ffc8d069 (patch)
tree4470480d904b65cf14ca524f96f79eca818c3eaf /REORG.TODO/libio/bug-memstream1.c
parent199fc19d3aaaf57944ef036e15904febe877fc93 (diff)
downloadglibc-zack/build-layout-experiment.tar.gz
glibc-zack/build-layout-experiment.tar.xz
glibc-zack/build-layout-experiment.zip
Prepare for radical source tree reorganization. zack/build-layout-experiment
All top-level files and directories are moved into a temporary storage
directory, REORG.TODO, except for files that will certainly still
exist in their current form at top level when we're done (COPYING,
COPYING.LIB, LICENSES, NEWS, README), all old ChangeLog files (which
are moved to the new directory OldChangeLogs, instead), and the
generated file INSTALL (which is just deleted; in the new order, there
will be no generated files checked into version control).
Diffstat (limited to 'REORG.TODO/libio/bug-memstream1.c')
-rw-r--r--REORG.TODO/libio/bug-memstream1.c134
1 files changed, 134 insertions, 0 deletions
diff --git a/REORG.TODO/libio/bug-memstream1.c b/REORG.TODO/libio/bug-memstream1.c
new file mode 100644
index 0000000000..d1ecc79564
--- /dev/null
+++ b/REORG.TODO/libio/bug-memstream1.c
@@ -0,0 +1,134 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+
+static int
+do_test (void)
+{
+  size_t size;
+  char *buf;
+  FILE *fp = open_memstream (&buf, &size);
+  if (fp == NULL)
+    {
+      puts ("open_memstream failed");
+      return 1;
+    }
+
+  off64_t off = ftello64 (fp);
+  if (off != 0)
+    {
+      puts ("initial position wrong");
+      return 1;
+    }
+
+  if (fseek (fp, 32768, SEEK_SET) != 0)
+    {
+      puts ("fseek failed");
+      return 1;
+    }
+
+  if (fputs ("foo", fp) == EOF)
+    {
+      puts ("fputs failed");
+      return 1;
+    }
+
+  if (fclose (fp) == EOF)
+    {
+      puts ("fclose failed");
+      return 1;
+    }
+
+  if (size != 32768 + 3)
+    {
+      printf ("expected size %d, got %zu\n", 32768 + 3, size);
+      return 1;
+    }
+
+  for (int i = 0; i < 32768; ++i)
+    if (buf[i] != '\0')
+      {
+	printf ("byte at offset %d is %#hhx\n", i, buf[i]);
+	return 1;
+      }
+
+  if (memcmp (buf + 32768, "foo", 3) != 0)
+    {
+      puts ("written string incorrect");
+      return 1;
+    }
+
+  /* Mark the buffer.  */
+  memset (buf, 'A', size);
+  free (buf);
+
+  /* Try again, this time with write mode enabled before the seek.  */
+  fp = open_memstream (&buf, &size);
+  if (fp == NULL)
+    {
+      puts ("2nd open_memstream failed");
+      return 1;
+    }
+
+  off = ftello64 (fp);
+  if (off != 0)
+    {
+      puts ("2nd initial position wrong");
+      return 1;
+    }
+
+  if (fputs ("bar", fp) == EOF)
+    {
+      puts ("2nd fputs failed");
+      return 1;
+    }
+
+  if (fseek (fp, 32768, SEEK_SET) != 0)
+    {
+      puts ("2nd fseek failed");
+      return 1;
+    }
+
+  if (fputs ("foo", fp) == EOF)
+    {
+      puts ("3rd fputs failed");
+      return 1;
+    }
+
+  if (fclose (fp) == EOF)
+    {
+      puts ("2nd fclose failed");
+      return 1;
+    }
+
+  if (size != 32768 + 3)
+    {
+      printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
+      return 1;
+    }
+
+  if (memcmp (buf, "bar", 3) != 0)
+    {
+      puts ("initial string incorrect in 2nd try");
+      return 1;
+    }
+
+  for (int i = 3; i < 32768; ++i)
+    if (buf[i] != '\0')
+      {
+	printf ("byte at offset %d is %#hhx in 2nd try\n", i, buf[i]);
+	return 1;
+      }
+
+  if (memcmp (buf + 32768, "foo", 3) != 0)
+    {
+      puts ("written string incorrect in 2nd try");
+      return 1;
+    }
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"