This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH v2] [BZ #12836] Make fmemopen(buf,size,"w+b") binary.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: libc-alpha at sourceware dot org
- Date: Tue, 14 May 2013 13:59:47 +0200
- Subject: [PATCH v2] [BZ #12836] Make fmemopen(buf,size,"w+b") binary.
- References: <20130509161648 dot GA31713 at domone dot kolej dot mff dot cuni dot cz> <Pine dot LNX dot 4 dot 64 dot 1305091636200 dot 25142 at digraph dot polyomino dot org dot uk>
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.
* libio/Makefile (tests): Updated.
* libio/test-fmemopen-bz13152.c: New file.
* libio/fmemopen.c (fmemopen): Make mode "w+b" binary.
---
libio/Makefile | 2 +-
libio/fmemopen.c | 6 ++++-
libio/test-fmemopen-bz13152.c | 49 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 55 insertions(+), 2 deletions(-)
create mode 100644 libio/test-fmemopen-bz13152.c
diff --git a/libio/Makefile b/libio/Makefile
index e15cd40..e3ed974 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-bz13152 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-bz13152.c b/libio/test-fmemopen-bz13152.c
new file mode 100644
index 0000000..7cfc09c
--- /dev/null
+++ b/libio/test-fmemopen-bz13152.c
@@ -0,0 +1,49 @@
+/* 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
+main (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;
+}
--
1.7.4.4