diff --git a/libio/memstream.c b/libio/memstream.c
index f83d4a5..3be5b58 100644
--- a/libio/memstream.c
+++ b/libio/memstream.c
@@ -31,13 +31,14 @@ struct _IO_FILE_memstream
 
 static int _IO_mem_sync (_IO_FILE* fp) __THROW;
 static void _IO_mem_finish (_IO_FILE* fp, int) __THROW;
+static int _IO_mem_overflow (_IO_FILE *fp, int c) __THROW;
 
 
 static const struct _IO_jump_t _IO_mem_jumps libio_vtable =
 {
   JUMP_INIT_DUMMY,
   JUMP_INIT (finish, _IO_mem_finish),
-  JUMP_INIT (overflow, _IO_str_overflow),
+  JUMP_INIT (overflow, _IO_mem_overflow),
   JUMP_INIT (underflow, _IO_str_underflow),
   JUMP_INIT (uflow, _IO_default_uflow),
   JUMP_INIT (pbackfail, _IO_str_pbackfail),
@@ -87,6 +88,7 @@ __open_memstream (char **bufloc, _IO_size_t *sizeloc)
       return NULL;
     }
   _IO_init_internal (&new_f->fp._sf._sbf._f, 0);
+  _IO_link_in ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf);
   _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps;
   _IO_str_init_static_internal (&new_f->fp._sf, buf, _IO_BUFSIZ, buf);
   new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
@@ -137,3 +139,14 @@ _IO_mem_finish (_IO_FILE *fp, int dummy)
 
   _IO_str_finish (fp, 0);
 }
+
+static int
+_IO_mem_overflow (_IO_FILE *fp, int c)
+{
+  int ret = _IO_str_overflow (fp, c);
+
+  struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
+  *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
+
+  return ret;
+}
diff --git a/libio/tst-memstream3.c b/libio/tst-memstream3.c
index ce201d1..395e5c2 100644
--- a/libio/tst-memstream3.c
+++ b/libio/tst-memstream3.c
@@ -23,6 +23,7 @@
 #include <stdarg.h>
 #include <errno.h>
 
+#include <support/check.h>
 
 #ifndef CHAR_T
 # define CHAR_T char
@@ -40,24 +41,9 @@
 static void
 mcheck_abort (enum mcheck_status ev)
 {
-  printf ("mecheck failed with status %d\n", (int) ev);
-  exit (1);
+  FAIL_EXIT1 ("mecheck failed with status %d", (int) ev);
 }
 
-static void
-error_printf (int line, const char *fmt, ...)
-{
-  va_list ap;
-
-  printf ("error: %s:%i: ", __FILE__, line);
-  va_start (ap, fmt);
-  vprintf (fmt, ap);
-  va_end (ap);
-}
-
-#define ERROR_RET1(...) \
-  { error_printf(__LINE__, __VA_ARGS__); return 1; }
-
 static int
 do_test_bz18241 (void)
 {
@@ -66,37 +52,37 @@ do_test_bz18241 (void)
 
   FILE *fp = OPEN_MEMSTREAM (&buf, &size);
   if (fp == NULL)
-    ERROR_RET1 ("%s failed\n", S(OPEN_MEMSTREAM));
+    FAIL_RET ("%s failed", S(OPEN_MEMSTREAM));
 
   if (FPUTC (W('a'), fp) != W('a'))
-    ERROR_RET1 ("%s failed (errno = %d)\n", S(FPUTC), errno);
+    FAIL_RET ("%s failed: %m", S(FPUTC));
   if (fflush (fp) != 0)
-    ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
+    FAIL_RET ("fflush failed: %m");
   if (fseek (fp, -2, SEEK_SET) != -1)
-    ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
+    FAIL_RET ("fseek failed: %m");
   if (errno != EINVAL)
-    ERROR_RET1 ("errno != EINVAL\n");
+    FAIL_RET ("errno != EINVAL");
   if (ftell (fp) != 1)
-    ERROR_RET1 ("ftell failed (errno = %d)\n", errno);
+    FAIL_RET ("ftell failed: %m");
   if (ferror (fp) != 0)
-    ERROR_RET1 ("ferror != 0\n");
+    FAIL_RET ("ferror != 0");
 
   if (fseek (fp, -1, SEEK_CUR) == -1)
-    ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
+    FAIL_RET ("fseek failed: %m");
   if (ftell (fp) != 0)
-    ERROR_RET1 ("ftell failed (errno = %d)\n", errno);
+    FAIL_RET ("ftell failed: %m");
   if (ferror (fp) != 0)
-    ERROR_RET1 ("ferror != 0\n");
+    FAIL_RET ("ferror != 0");
   if (FPUTC (W('b'), fp) != W('b'))
-    ERROR_RET1 ("%s failed (errno = %d)\n", S(FPUTC), errno);
+    FAIL_RET ("%s failed: %m", S(FPUTC));
   if (fflush (fp) != 0)
-    ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
+    FAIL_RET ("fflush failed: %m");
 
   if (fclose (fp) != 0)
-    ERROR_RET1 ("fclose failed (errno = %d\n", errno);
+    FAIL_RET ("fclose failed: %m");
 
   if (STRCMP (buf, W("b")) != 0)
-    ERROR_RET1 ("%s failed\n", S(STRCMP));
+    FAIL_RET ("%s failed", S(STRCMP));
 
   free (buf);
 
@@ -112,45 +98,74 @@ do_test_bz20181 (void)
 
   FILE *fp = OPEN_MEMSTREAM (&buf, &size);
   if (fp == NULL)
-    ERROR_RET1 ("%s failed\n", S(OPEN_MEMSTREAM));
+    FAIL_RET ("%s failed\n", S(OPEN_MEMSTREAM));
 
   if ((ret = FWRITE (W("abc"), 1, 3, fp)) != 3)
-    ERROR_RET1 ("%s failed (errno = %d)\n", S(FWRITE), errno);
+    FAIL_RET ("%s failed (errno = %d)\n", S(FWRITE), errno);
 
   if (fseek (fp, 0, SEEK_SET) != 0)
-    ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
+    FAIL_RET ("fseek failed (errno = %d)\n", errno);
 
   if (FWRITE (W("z"), 1, 1, fp) != 1)
-    ERROR_RET1 ("%s failed (errno = %d)\n", S(FWRITE), errno);
+    FAIL_RET ("%s failed (errno = %d)\n", S(FWRITE), errno);
 
   if (fflush (fp) != 0)
-    ERROR_RET1 ("fflush failed (errno = %d)\n", errno);
+    FAIL_RET ("fflush failed (errno = %d)\n", errno);
 
   /* Avoid truncating the buffer on close.  */
   if (fseek (fp, 3, SEEK_SET) != 0)
-    ERROR_RET1 ("fseek failed (errno = %d)\n", errno);
+    FAIL_RET ("fseek failed (errno = %d)\n", errno);
 
   if (fclose (fp) != 0)
-    ERROR_RET1 ("fclose failed (errno = %d\n", errno);
+    FAIL_RET ("fclose failed (errno = %d\n", errno);
 
   if (size != 3)
-    ERROR_RET1 ("size != 3\n");
+    FAIL_RET ("size != 3\n");
 
   if (buf[0] != W('z')
       || buf[1] != W('b')
       || buf[2] != W('c'))
     {
       PRINTF (W("error: buf {%c,%c,%c} != {z,b,c}\n"),
-	      buf[0], buf[1], buf[2]);
-      return 1;
+             buf[0], buf[1], buf[2]);
+      FAIL_RET (NULL);
     }
 
+
   free (buf);
 
   return 0;
 }
 
 static int
+do_test_bz21735 (void)
+{
+  CHAR_T *buf;
+  size_t size;
+
+  FILE *fp = OPEN_MEMSTREAM (&buf, &size);
+  if (fp == NULL)
+    FAIL_RET ("%s failed", S(OPEN_MEMSTREAM));
+
+  if (FPUTC (W('a'), fp) != W('a'))
+    FAIL_RET ("%s failed: %m", S(FPUTC));
+
+  if (fflush (fp) != 0)
+    FAIL_RET ("fflush failed: %m");
+
+  if (size != 1)
+    FAIL_RET ("size != 1");
+
+  if (fileno (fp) != -1)
+    FAIL_RET ("fileno returned a valid integer descriptor");
+
+  if (fclose (fp) != 0)
+    FAIL_RET ("fclose: %m");
+
+  return 0;
+}
+
+static int
 do_test (void)
 {
   int ret = 0;
@@ -159,9 +174,9 @@ do_test (void)
 
   ret += do_test_bz18241 ();
   ret += do_test_bz20181 ();
+  ret += do_test_bz21735 ();
 
   return ret;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>
diff --git a/libio/wmemstream.c b/libio/wmemstream.c
index 5bc77f5..f6d9f14 100644
--- a/libio/wmemstream.c
+++ b/libio/wmemstream.c
@@ -32,13 +32,14 @@ struct _IO_FILE_wmemstream
 
 static int _IO_wmem_sync (_IO_FILE* fp) __THROW;
 static void _IO_wmem_finish (_IO_FILE* fp, int) __THROW;
+static int _IO_wmem_overflow (_IO_FILE *fp, int c) __THROW;
 
 
 static const struct _IO_jump_t _IO_wmem_jumps libio_vtable =
 {
   JUMP_INIT_DUMMY,
   JUMP_INIT (finish, _IO_wmem_finish),
-  JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow),
+  JUMP_INIT (overflow, (_IO_overflow_t) _IO_wmem_overflow),
   JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow),
   JUMP_INIT (uflow, (_IO_underflow_t) _IO_wdefault_uflow),
   JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
@@ -89,6 +90,7 @@ open_wmemstream (wchar_t **bufloc, _IO_size_t *sizeloc)
     }
   _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
   _IO_fwide (&new_f->fp._sf._sbf._f, 1);
+  _IO_link_in ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf);
   _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf,
 			_IO_BUFSIZ / sizeof (wchar_t), buf);
   new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF;
@@ -142,3 +144,15 @@ _IO_wmem_finish (_IO_FILE *fp, int dummy)
 
   _IO_wstr_finish (fp, 0);
 }
+
+static int
+_IO_wmem_overflow (_IO_FILE *fp, int c)
+{
+  int ret = _IO_wstr_overflow (fp, c);
+
+  struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
+  *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
+		  - fp->_wide_data->_IO_write_base);
+
+  return ret;
+}
