about summary refs log tree commit diff
path: root/libio/bug-memstream1.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2006-08-14 22:16:14 +0000
committerUlrich Drepper <drepper@redhat.com>2006-08-14 22:16:14 +0000
commit107b8a922a9f72bae8d066549c675062bee0897f (patch)
treefd03a2fabf180e1abd1cfaef284447bdf63cad6f /libio/bug-memstream1.c
parent899937348376faa9092f123ffe7f000df0604b36 (diff)
downloadglibc-107b8a922a9f72bae8d066549c675062bee0897f.tar.gz
glibc-107b8a922a9f72bae8d066549c675062bee0897f.tar.xz
glibc-107b8a922a9f72bae8d066549c675062bee0897f.zip
[BZ #1996]
2006-08-14  Ulrich Drepper  <drepper@redhat.com>
	[BZ #1996]
	* libio/memstream.c (open_memstream): Allocate initial buffer with
	calloc.
	* libio/wmemstream.c (open_wmemstream): Likewise.
	* libio/strops.c: Pretty printing.
	(_IO_str_overflow): Clear uninitialized part of the new buffer.
	(enlarge_userbuf): New function.
	(_IO_str_seekoff): Call it if seek position is larger than current
	buffer.
	* libio/wstrops.c: Likewise.
	* libio/vasprintf.c: Add comment as to why we do not have to use
	calloc instead of malloc to allocate initial buffer.
	* libio/Makefile (tests): Add bug-memstream1 and bug-wmemstream1.
	* libio/bug-memstream1.c: New file.
	* libio/bug-wmemstream1.c: New file.
Diffstat (limited to 'libio/bug-memstream1.c')
-rw-r--r--libio/bug-memstream1.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/libio/bug-memstream1.c b/libio/bug-memstream1.c
new file mode 100644
index 0000000000..8af36fee2d
--- /dev/null
+++ b/libio/bug-memstream1.c
@@ -0,0 +1,133 @@
+#include <stdio.h>
+#include <string.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"