about summary refs log tree commit diff
path: root/stdio-common
diff options
context:
space:
mode:
authorAdhemerval Zanella <azanella@linux.vnet.ibm.com>2014-06-09 16:19:25 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.com>2015-07-08 12:07:21 -0300
commitfdb7d390dd0d96e4a8239c46f3aa64598b90842b (patch)
treeadab2a28bad088043828d25b5c6bf83a5612fba7 /stdio-common
parent1c1e3125206ef810dc7282023f6267a33b486233 (diff)
downloadglibc-fdb7d390dd0d96e4a8239c46f3aa64598b90842b.tar.gz
glibc-fdb7d390dd0d96e4a8239c46f3aa64598b90842b.tar.xz
glibc-fdb7d390dd0d96e4a8239c46f3aa64598b90842b.zip
libio: fmemopen rewrite to POSIX compliance
This patch added a new fmemopen version, for glibc 2.22, that aims to be
POSIX complaint.  It fixes some long-stading glibc fmemopen issues, such
as:

* it changes the way fseek with SEEK_END works on fmemopen to seek
  relative to buffer size instead of first '\0'.  This is default mode and
  'b' opening mode does not change internal behavior (bz#6544).

* fix apending opening mode to use as start position either first null
  byte of len specified in function call (bz#13152 and #13151).

* remove binary option 'b' and internal different handling (bz#12836)

* fix seek/SEE_END with negative values (bz#14292).

A compatibility symbol is provided to with old behavior for older symbols
version (2.2.5).

	* include/stdio.h (fmemopen): Remove hidden prototype.
	(__fmemopen): Add new hidden prototype.
	* libio/Makefile: Add oldfmemopen object.
	* libio/Versions [GLIBC_2.22]: Add new fmemopen symbol.
	* libio/fmemopen.c (__fmemopen): Function rewrite to be POSIX
	compliance.
	* libio/oldfmemopen.c: New file: old fmemopen implementation for
	symbol compatibility.
	* stdio-common/Makefile [tests]: Add new tst-fmemopen3.
	* stdio-common/psiginfo.c [psiginfo]: Call __fmemopen instead of
	fmemopen.
	* stdio-common/tst-fmemopen3.c: New file: more fmemopen tests, focus
	on append and read mode.
	* sysdeps/unix/sysv/linux/aarch64/libc.abilist [GLIBC_2.22]: Add
	fmemopen.
	* sysdeps/unix/sysv/linux/alpha/libc.abilist [GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/arm/libc.abilist [GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/i386/libc.abilist [GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/ia64/libc.abilist [GLIBC_2.22]:
	Likewise.
	* sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist [GLIBC_2.22]:
	Likewise.
	* sysdeps/unix/sysv/linux/microblaze/libc.abilist [GLIBC_2.22]:
	Likewise.
	* sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist [GLIBC_2.22]:
	Likewise.
	* sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist [GLIBC_2.22]:
	Likewise.
	* sysdeps/unix/sysv/linux/sh/libc.abilist [GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
	[GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist [GLIBC_2.22]:
	Likewise.
	* sysdeps/unix/sysv/linux/x86_64/64/libc.abilist [GLIBC_2.22]:
	Likewise.
	* sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist [GLIBC_2.22]:
	Likewise.
	* sysdeps/unix/sysv/linux/hppa/libc.abilist [GLIBC_2.22]: Likewise.
	* sysdeps/unix/sysv/linux/nios2/libc.abilist [GLIBC_2.22]: Likewise.
Diffstat (limited to 'stdio-common')
-rw-r--r--stdio-common/Makefile2
-rw-r--r--stdio-common/psiginfo.c2
-rw-r--r--stdio-common/tst-fmemopen3.c206
3 files changed, 208 insertions, 2 deletions
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index 8567056402..d0bf0e1083 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -57,7 +57,7 @@ tests := tstscanf test_rdwr test-popen tstgetln test-fseek \
 	 bug19 bug19a tst-popen2 scanf13 scanf14 scanf15 bug20 bug21 bug22 \
 	 scanf16 scanf17 tst-setvbuf1 tst-grouping bug23 bug24 \
 	 bug-vfprintf-nargs tst-long-dbl-fphex tst-fphex-wide tst-sprintf3 \
-	 bug25 tst-printf-round bug23-2 bug23-3 bug23-4 bug26
+	 bug25 tst-printf-round bug23-2 bug23-3 bug23-4 bug26 tst-fmemopen3
 
 test-srcs = tst-unbputc tst-printf
 
diff --git a/stdio-common/psiginfo.c b/stdio-common/psiginfo.c
index 3aff5012d3..30847ea6aa 100644
--- a/stdio-common/psiginfo.c
+++ b/stdio-common/psiginfo.c
@@ -60,7 +60,7 @@ void
 psiginfo (const siginfo_t *pinfo, const char *s)
 {
   char buf[512];
-  FILE *fp = fmemopen (buf, sizeof (buf), "w");
+  FILE *fp = __fmemopen (buf, sizeof (buf), "w");
   if (fp == NULL)
     {
       const char *colon;
diff --git a/stdio-common/tst-fmemopen3.c b/stdio-common/tst-fmemopen3.c
new file mode 100644
index 0000000000..be481539ca
--- /dev/null
+++ b/stdio-common/tst-fmemopen3.c
@@ -0,0 +1,206 @@
+/* fmemopen tests for append and read mode.
+   Copyright (C) 2015 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 <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+
+static void
+print_buffer (const char *s, size_t n)
+{
+  size_t i;
+  for (i=0; i<n; ++i)
+    printf ("0x%02X (%c), ", s[i], s[i]);
+}
+
+/* This test check append mode initial position (a/a+) based on POSIX defition
+   (BZ#6544 and BZ#13151).  */
+static int
+do_test_write_append (const char *mode)
+{
+  char buf[32] = "testing buffer";
+  char exp[32] = "testing bufferXX";
+
+  FILE *fp = fmemopen (buf, sizeof (buf), mode);
+
+  fflush (fp);
+  fprintf (fp, "X");
+  fseek (fp, 0, SEEK_SET);
+  fprintf (fp, "X");
+  fclose (fp);
+
+  if (strcmp (buf, exp) != 0)
+    {
+      printf ("%s: check failed: %s != %s\n", __FUNCTION__, buf, exp);
+      return 1;
+    }
+
+  return 0;
+}
+
+/* This test check append mode initial position (a/a+) based on POSIX defition
+   (BZ#6544 and BZ#13151) for buffer without null byte end.  */
+static int
+do_test_write_append_without_null (const char *mode)
+{
+  char buf[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
+  char exp[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
+
+  /* If '\0' is not found in buffer, POSIX states that SEEK_SET should be
+     the size argument.  */
+  FILE *fp = fmemopen (buf, sizeof (buf) - 2, "a");
+
+  fflush (fp);
+  fputc (0x70, fp);
+  fseek (fp, 0, SEEK_SET);
+  fputc (0x70, fp);
+  fputc (0x70, fp);
+  fclose (fp);
+
+  /* POSIX also states that a write operation on the stream shall not advance
+     the current buffer size beyond the size given in fmemopen, so the string
+     should be same.  */
+  if (memcmp (buf, exp, sizeof (buf)) != 0)
+    {
+      printf ("%s: check failed: ", __FUNCTION__);
+      print_buffer (buf, sizeof (buf));
+      printf ("!= ");
+      print_buffer (exp, sizeof (exp));
+      printf ("\n");
+      return 1;
+    }
+
+  return 0;
+}
+
+/* This test check for initial position and feek value for fmemopen objects
+   opened with append mode.  */
+static int
+do_test_read_append (void)
+{
+  char buf[32] = "testing buffer";
+  size_t buflen = strlen (buf);
+  long fpos;
+
+  /* POSIX defines for 'a+' the initial position is the first null byte.  */
+  FILE *fp = fmemopen (buf, sizeof (buf), "a+");
+
+  fpos = ftell (fp);
+  if (fpos != buflen)
+    {
+      printf ("%s: ftell|SEEK_SET (fp) %li != strlen (%s) %zu\n",
+	      __FUNCTION__, fpos, buf, buflen);
+      fclose (fp);
+      return 1;
+    }
+
+  fseek (fp, 0, SEEK_END);
+
+  if (fpos != buflen)
+    {
+      printf ("%s: ftell|SEEK_END (fp) %li != strlen (%s) %zu\n",
+	      __FUNCTION__, fpos, buf, buflen);
+      fclose (fp);
+      return 1;
+    }
+  fclose (fp);
+
+  /* Check if attempting to read past the current size, defined as strlen (buf)
+     yield an EOF.  */
+  fp = fmemopen (buf, sizeof (buf), "a+");
+  if (getc(fp) != EOF)
+    {
+      printf ("%s: getc(fp) != EOF\n", __FUNCTION__);
+      fclose (fp);
+      return -1;
+    }
+
+  fclose (fp);
+
+  return 0;
+}
+
+/* This test check for fseek (SEEK_END) using negative offsets (BZ#14292).  The
+   starting position of descriptor is different base on the opening mode.  */
+static int
+do_test_read_seek_neg (const char *mode, const char *expected)
+{
+  char buf[] = "abcdefghijklmnopqrstuvxz0123456789";
+  char tmp[10];
+  size_t tmps = sizeof (tmps);
+  long offset = -11;
+
+  FILE *fp = fmemopen (buf, sizeof (buf), mode);
+  fseek (fp, offset, SEEK_END);
+  fread (tmp, tmps, 1, fp);
+
+  if (memcmp (tmp, expected, tmps) != 0)
+    {
+      printf ("%s: fmemopen(%s) - fseek (fp, %li, SEEK_END):\n",
+	      __FUNCTION__, mode, offset);
+      printf ("  returned: ");
+      print_buffer (tmp, tmps);
+      printf ("\n");
+      printf ("  expected: ");
+      print_buffer (expected, tmps);
+      printf ("\n");
+      return 1;
+    }
+
+  fclose (fp);
+
+  return 0;
+}
+
+static int
+do_test_read_seek_negative (void)
+{
+  int ret = 0;
+
+  /* 'r' and 'w' modes defines the initial position at the buffer start and
+     seek with SEEK_END shall seek relative to its size give in fmemopen
+     call.  The expected tmp result is 0 to 9 *without* the ending null  */
+  ret += do_test_read_seek_neg ("r", "0123456789");
+  /* 'a+' mode sets the initial position at the first null byte in buffer and
+    SEEK_END shall seek relative to its size as well.  The expected result is
+    z012345678, since SEEK_END plus a+ start at '\0', not size.  */
+  ret += do_test_read_seek_neg ("a+", "z012345678");
+
+  return ret;
+}
+
+static int
+do_test (void)
+{
+  int ret = 0;
+
+  ret += do_test_write_append ("a");
+  ret += do_test_write_append_without_null ("a");
+  ret += do_test_write_append ("a+");
+  ret += do_test_write_append_without_null ("a+");
+
+  ret += do_test_read_append ();
+
+  ret += do_test_read_seek_negative ();
+
+  return ret;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"