]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
libio: Fix fmemopen 'w' mode with provided buffer
authorAdhemerval Zanella <adhemerval.zanella@linaro.com>
Wed, 15 Jul 2015 19:15:47 +0000 (16:15 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.com>
Thu, 16 Jul 2015 18:21:49 +0000 (15:21 -0300)
If 'w' mode is used with a provided buffer the fmemopen will try to find
the first null byte to set as maximum internal stream size.  It should be
done only for append mode ('a').

Kudos for Stefan Liebler for finding this error on s390-32.

* libio/fmemopen.c (__fmemopen): Fix 'w' openmode with provided
buffer.
* stdio-common/tst-fmemopen2.c (do_test_with_buffer): Fix typo and
fail output information.

ChangeLog
libio/fmemopen.c
stdio-common/tst-fmemopen2.c

index 809fe1807a9436e99054ef0edca8210179b52096..17f33774deb742cb8c61204ca1ca2d7aed6622e8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2015-07-16  Adhemerval Zanella  <adhemerval.zanella@linaro.org>
+
+       * libio/fmemopen.c (__fmemopen): Fix 'w' openmode with provided
+       buffer.
+       * stdio-common/tst-fmemopen2.c (do_test_with_buffer): Fix typo and
+       fail output information.
+
 2015-07-16  Rajalakshmi Srinivasaraghavan  <raji@linux.vnet.ibm.com>
 
        * sysdeps/powerpc/powerpc64/multiarch/Makefile: Add strstr-power7
index e6e6a49f76d269855235d3c89a889bb61baa761c..3ab3e8dac48ad4bbb1fc4653793e422b808a2421 100644 (file)
@@ -150,7 +150,7 @@ __fmemopen (void *buf, size_t len, const char *mode)
   cookie_io_functions_t iof;
   fmemopen_cookie_t *c;
 
-  c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
+  c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1);
   if (c == NULL)
     return NULL;
 
@@ -165,7 +165,6 @@ __fmemopen (void *buf, size_t len, const char *mode)
          return NULL;
        }
       c->buffer[0] = '\0';
-      c->maxpos = 0;
     }
   else
     {
@@ -182,7 +181,8 @@ __fmemopen (void *buf, size_t len, const char *mode)
       if (mode[0] == 'w' && mode[1] == '+')
        c->buffer[0] = '\0';
 
-      c->maxpos = strnlen (c->buffer, len);
+      if (mode[0] == 'a')
+        c->maxpos = strnlen (c->buffer, len);
     }
 
 
index 16dd3ad73dc1d23a12ab1660db2518cb6df4de85..a2c05c12df3fbcc90b82c96438c38b0d4503e9bf 100644 (file)
@@ -34,7 +34,7 @@ do_test_with_buffer (void)
   FILE *fp = fmemopen (buf, nbuf, "w");
   if (fp == NULL)
     {
-      printf ("FAIL: fmemopen failedi (%s)\n", __FUNCTION__);
+      printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__);
       return 1;
     }
 
@@ -69,7 +69,7 @@ do_test_with_buffer (void)
   if (o != nstr)
     {
       printf ("FAIL: third ftello returned %jd, expected %zu\n",
-             (intmax_t)o, nbuf);
+             (intmax_t)o, nstr);
       result = 1;
     }