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 v2][BZ #13152] fmemopen does not honor append mode.


On Thu, May 09, 2013 at 04:55:02PM +0000, Joseph S. Myers wrote:
> As usual, for user-visible bugs like this one that should be 
> straightforward to test in the glibc testsuite, please include a testcase 
> that fails before and passes after the patch to the implementation.
> 
Version with added testcase (needs commit 13152 first).
I noticed that I swapped testcase bz numbers, but will commit them correctly.

Could somebody coment backward compatibility?

Ondra

	* libio/Makefile (tests): Update.
	* libio/test-fmemopen-bz13152.c: New file.
	* libio/fmemopen.c (fmemopen): Honor append mode.

---
 libio/Makefile                |    2 +-
 libio/fmemopen.c              |   15 +++++++-----
 libio/test-fmemopen-bz12836.c |   49 +++++++++++++++++++++++++++++++++++++++++
 libio/test-fmemopen-bz13152.c |   41 +++++++++++++++-------------------
 4 files changed, 77 insertions(+), 30 deletions(-)
 create mode 100644 libio/test-fmemopen-bz12836.c

diff --git a/libio/Makefile b/libio/Makefile
index d134815..07632ed 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 test-fopen-bz12685 bug-fopena+ bug-wfflush \
+	tst-mmap2-eofsync tst-mmap-offend test-fopen-bz12685 test-fmemopen-bz13152 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/fmemopen.c b/libio/fmemopen.c
index 09effee..6af9309 100644
--- a/libio/fmemopen.c
+++ b/libio/fmemopen.c
@@ -241,15 +241,18 @@ fmemopen (void *buf, size_t len, const char *mode)
 	c->buffer[0] = '\0';
 
       c->maxpos = strnlen (c->buffer, len);
-    }
 
+      if (mode[0] == 'a')
+	{
+	  len -= c->maxpos;
+	  c->buffer += c->maxpos;
+	  c->maxpos = 0;
+	}
+     }
+ 
+  c->pos = 0;
   c->size = len;
 
-  if (mode[0] == 'a')
-    c->pos = c->maxpos;
-  else
-    c->pos = 0;
-
   c->binmode = 0;
   for (i = 0; mode[i]; i++)
     if (mode[i] == 'b')
diff --git a/libio/test-fmemopen-bz12836.c b/libio/test-fmemopen-bz12836.c
new file mode 100644
index 0000000..cf10543
--- /dev/null
+++ b/libio/test-fmemopen-bz12836.c
@@ -0,0 +1,49 @@
+/* Test for binary mode see bug 12836.
+   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;
+}
diff --git a/libio/test-fmemopen-bz13152.c b/libio/test-fmemopen-bz13152.c
index 7cfc09c..ff6f9ca 100644
--- a/libio/test-fmemopen-bz13152.c
+++ b/libio/test-fmemopen-bz13152.c
@@ -16,34 +16,29 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-static char buffer[] = "foobar";
-static char s[]      = "abc";
-
+#define _XOPEN_SOURCE 700
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
+#include <stdlib.h>
 
 int
-main (void)
+main(int argc, char **argv)
 {
-  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;
-    }
+  char buf[20] = {"hello, world"};
+  FILE *fp = fmemopen(buf, 20, "a+");
+  char c;
+  int j;
+  fseek(fp, 0, SEEK_SET);
+
+  fflush(fp);
+  fprintf(fp, "X");
+  fclose(fp);
+  
+  if (buf[0] != 'h')
+    return 1;
 
   return 0;
 }
+
+
-- 
1.7.4.4


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