This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v3][BZ #12685] Allow arbitrary number of modes in fopen.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Andreas Schwab <schwab at suse dot de>
- Cc: "Joseph S. Myers" <joseph at codesourcery dot com>, Carlos O'Donell <carlos at redhat dot com>, libc-alpha at sourceware dot org
- Date: Wed, 15 May 2013 11:17:35 +0200
- Subject: Re: [PATCH v3][BZ #12685] Allow arbitrary number of modes in fopen.
- References: <20130509152046 dot GA4242 at domone dot kolej dot mff dot cuni dot cz> <Pine dot LNX dot 4 dot 64 dot 1305091533180 dot 25142 at digraph dot polyomino dot org dot uk> <518BEFB2 dot 80304 at redhat dot com> <20130510103330 dot GA12338 at domone dot kolej dot mff dot cuni dot cz> <Pine dot LNX dot 4 dot 64 dot 1305101139470 dot 1189 at digraph dot polyomino dot org dot uk> <20130514121540 dot GA28892 at domone dot kolej dot mff dot cuni dot cz> <mvm38tpzqst dot fsf at hawking dot suse dot de> <20130514124257 dot GB18821 at domone dot kolej dot mff dot cuni dot cz> <mvmy5bhyb4h dot fsf at hawking dot suse dot de>
On Tue, May 14, 2013 at 02:55:10PM +0200, Andreas Schwab wrote:
> OndÅej BÃlka <neleai@seznam.cz> writes:
>
> > On Tue, May 14, 2013 at 02:31:14PM +0200, Andreas Schwab wrote:
> >> OndÅej BÃlka <neleai@seznam.cz> writes:
> >>
> >> > diff --git a/libio/fileops.c b/libio/fileops.c
> >> > index 61b61b3..50c3eb2 100644
> >> > --- a/libio/fileops.c
> >> > +++ b/libio/fileops.c
> >> > @@ -286,12 +286,10 @@ _IO_new_file_fopen (fp, filename, mode, is32not64)
> >> > #ifdef _LIBC
> >> > last_recognized = mode;
> >> > #endif
> >> > - for (i = 1; i < 7; ++i)
> >> > + while (*mode && *mode != ',')
> >> > {
> >> > switch (*++mode)
> >>
> >> That reads mode[1], but the loop conditions tests mode[0].
> >>
> > This is still valid, to preserve original control flow.
>
> But useless, since '\0' and ',' are processed before being tested for.
> You should increment in the loop condition.
I moved it.
* libio/fileops.c (_IO_new_file_fopen): Allow arbitrary number of modes.
* libio/iofdopen.c (_IO_new_fdopen): Likewise.
* libio/test-fopen-bz12685.c: New file.
* libio/Makefile (tests): Updated.
---
libio/Makefile | 2 +-
libio/fileops.c | 6 +---
libio/iofdopen.c | 6 +---
libio/test-fopen-bz12685.c | 50 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 55 insertions(+), 9 deletions(-)
create mode 100644 libio/test-fopen-bz12685.c
diff --git a/libio/Makefile b/libio/Makefile
index e3ed974..d134815 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -54,7 +54,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \
tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof \
tst-freopen bug-rewind bug-rewind2 bug-ungetc bug-fseek \
tst-mmap-eofsync tst-mmap-fflushsync bug-mmap-fflush \
- tst-mmap2-eofsync tst-mmap-offend bug-fopena+ bug-wfflush \
+ tst-mmap2-eofsync tst-mmap-offend test-fopen-bz12685 bug-fopena+ bug-wfflush \
bug-ungetc2 bug-ftell bug-ungetc3 bug-ungetc4 tst-fopenloc2 \
tst-memstream1 tst-memstream2 \
tst-wmemstream1 tst-wmemstream2 \
diff --git a/libio/fileops.c b/libio/fileops.c
index 61b61b3..b33490a 100644
--- a/libio/fileops.c
+++ b/libio/fileops.c
@@ -286,12 +286,10 @@ _IO_new_file_fopen (fp, filename, mode, is32not64)
#ifdef _LIBC
last_recognized = mode;
#endif
- for (i = 1; i < 7; ++i)
+ while (*++mode && *mode != ',')
{
- switch (*++mode)
+ switch (*mode)
{
- case '\0':
- break;
case '+':
omode = O_RDWR;
read_write &= _IO_IS_APPENDING;
diff --git a/libio/iofdopen.c b/libio/iofdopen.c
index a65a5a6..10db2e0 100644
--- a/libio/iofdopen.c
+++ b/libio/iofdopen.c
@@ -75,12 +75,10 @@ _IO_new_fdopen (fd, mode)
MAYBE_SET_EINVAL;
return NULL;
}
- for (i = 1; i < 5; ++i)
+ while (*++mode && *mode != ',')
{
- switch (*++mode)
+ switch (*mode)
{
- case '\0':
- break;
case '+':
read_write &= _IO_IS_APPENDING;
break;
diff --git a/libio/test-fopen-bz12685.c b/libio/test-fopen-bz12685.c
new file mode 100644
index 0000000..c5ced4b
--- /dev/null
+++ b/libio/test-fopen-bz12685.c
@@ -0,0 +1,50 @@
+/* Test for fopen and fdopen modes, see bug 12685.
+ 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/>. */
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+int
+main (void)
+{
+ int i;
+ FILE *stream, *stream2;
+
+ /* Test if fopen mode e that sets FD_CLOEXEC is recognized. */
+ stream = fopen ("/dev/null", "rccccccccccccccccccccce");
+
+ if ((fcntl (fileno (stream), F_GETFD) & FD_CLOEXEC) == 0)
+ {
+ printf("no FD_CLOEXEC on fopen\n");
+ return 1;
+ }
+ /* In fdopen e flag is ignored. */
+ /*
+ stream2 = fdopen (fileno(fopen("/dev/null", "r"))
+ , "rccccccccccccccccccccce");
+ if ((fcntl (fileno (stream2), F_GETFD) & FD_CLOEXEC) == 0)
+ {
+ printf("no FD_CLOEXEC on fdopen\n");
+ return 1;
+ }
+ */
+
+ return 0;
+}
--
1.7.4.4