This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v3] [BZ #12836] Make fmemopen(buf,size,"w+b") binary.


On Tue, May 14, 2013 at 01:59:47PM +0200, OndÅej BÃlka wrote:
> On Thu, May 09, 2013 at 04:37:47PM +0000, Joseph S. Myers wrote:
> > On Thu, 9 May 2013, Ondrej Bilka wrote:
> > 
> > > Posting Eric Blake patch from
> > > http://sourceware.org/bugzilla/show_bug.cgi?id=12836
> > > 
> > > OK to commit?
> > > 
> > 
> > As usual, if the bug was user-visible in some way readily testable in the 
> > testsuite (as opposed to needing out-of-memory conditions, difficult to 
> > generate races among large numbers of threads, ...), the patch should add 
> > a testcase for it.
> > 
> Here is updated patch with testcase.
> 
testcase with skeleton.

 	* libio/Makefile (tests): Updated.
 	* libio/test-fmemopen-bz12836.c: New file.
 	* libio/fmemopen.c (fmemopen): Make mode "w+b" binary.



---
 libio/Makefile                |    2 +-
 libio/fmemopen.c              |    6 ++++-
 libio/test-fmemopen-bz12836.c |   51 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 2 deletions(-)
 create mode 100644 libio/test-fmemopen-bz12836.c

diff --git a/libio/Makefile b/libio/Makefile
index e15cd40..e8af20b 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -49,7 +49,7 @@ routines	:=							      \
 include ../Makeconfig
 
 tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
-	tst_wprintf2 tst-widetext test-fmemopen tst-ext tst-ext2 \
+	tst_wprintf2 tst-widetext test-fmemopen test-fmemopen-bz12836 tst-ext tst-ext2 \
 	tst-fgetws tst-ungetwc1 tst-ungetwc2 tst-swscanf tst-sscanf	      \
 	tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof          \
 	tst-freopen bug-rewind bug-rewind2 bug-ungetc bug-fseek \
diff --git a/libio/fmemopen.c b/libio/fmemopen.c
index 02c764f..09effee 100644
--- a/libio/fmemopen.c
+++ b/libio/fmemopen.c
@@ -201,6 +201,7 @@ fmemopen (void *buf, size_t len, const char *mode)
 {
   cookie_io_functions_t iof;
   fmemopen_cookie_t *c;
+  int i;
 
   if (__builtin_expect (len == 0, 0))
     {
@@ -249,7 +250,10 @@ fmemopen (void *buf, size_t len, const char *mode)
   else
     c->pos = 0;
 
-  c->binmode = mode[0] != '\0' && mode[1] == 'b';
+  c->binmode = 0;
+  for (i = 0; mode[i]; i++)
+    if (mode[i] == 'b')
+      c->binmode = 1;
 
   iof.read = fmemopen_read;
   iof.write = fmemopen_write;
diff --git a/libio/test-fmemopen-bz12836.c b/libio/test-fmemopen-bz12836.c
new file mode 100644
index 0000000..d891008
--- /dev/null
+++ b/libio/test-fmemopen-bz12836.c
@@ -0,0 +1,51 @@
+/* Test for binary mode see bug 13152.
+   Copyright (C) 2013 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+static char buffer[] = "foobar";
+static char s[]      = "abc";
+
+#include <stdio.h>
+#include <string.h>
+
+int
+do_test (void)
+{
+  int i;
+  FILE *stream;
+
+  stream = fmemopen (buffer, strlen (buffer), "w+b");
+	fprintf (stream, s);
+  fclose (stream);
+
+	for (i = 0 ; i < strlen (s) ; i++)
+    if (buffer[i] != s[i])
+      {
+        printf ("Disagree at position %i.\n",i);
+        return 1;
+      }
+
+  if (buffer[strlen (s)] == '\0')
+    {
+      printf ("Not opened in binary mode.\n");
+      return 1;
+    }
+
+  return 0;
+}
+
+#include "../test-skeleton.c"
-- 
1.7.4.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]