]> sourceware.org Git - glibc.git/commitdiff
Don't error out writing a multibyte character to an unbuffered stream (bug 17522)
authorAndreas Schwab <schwab@suse.de>
Thu, 30 Oct 2014 11:18:48 +0000 (12:18 +0100)
committerAndreas Schwab <schwab@suse.de>
Mon, 3 Nov 2014 08:58:24 +0000 (09:58 +0100)
ChangeLog
NEWS
libio/Makefile
libio/tst-fputws.c [new file with mode: 0644]
libio/wfileops.c

index 64aba4a9f1f485ba183d1b6461a19ee7a63e6e62..ddcb44341a4bf3283cd2a2d6c482dfcd2b2e8a03 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2014-11-03  Andreas Schwab  <schwab@suse.de>
+
+       [BZ #17522]
+       * libio/wfileops.c (_IO_wdo_write): If the file buffer has room
+       for less than MB_LEN_MAX use a local buffer of that size.
+       * libio/tst-fputws.c: New file.
+       * libio/Makefile (tests): Add tst-fputws.
+
 2014-11-01  Jose E. Marchesi  <jose.marchesi@oracle.com>
 
        * sysdeps/unix/sysv/linux/sparc/sys/ucontext.h (struct fpu): fix
diff --git a/NEWS b/NEWS
index f4cd69fe5924ba5324543f8ab0d7640d7d2faff9..382d38dbdfd322cf66d0984f1e24279c6981435c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,7 +10,7 @@ Version 2.21
 * The following bugs are resolved with this release:
 
   6652, 12926, 14138, 14171, 15215, 15884, 17266, 17344, 17363, 17370,
-  17371, 17411, 17460, 17485, 17501, 17508.
+  17371, 17411, 17460, 17485, 17501, 17508, 17522.
 \f
 Version 2.20
 
index 56952ce74473caf1960380ffa964d714f0dedfa4..2742128a9df8c735eb5d834a526f7752b1b44e9a 100644 (file)
@@ -61,7 +61,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
        bug-memstream1 bug-wmemstream1 \
        tst-setvbuf1 tst-popen1 tst-fgetwc bug-wsetpos tst-fseek \
        tst-fwrite-error tst-ftell-partial-wide tst-ftell-active-handler \
-       tst-ftell-append
+       tst-ftell-append tst-fputws
 ifeq (yes,$(build-shared))
 # Add test-fopenloc only if shared library is enabled since it depends on
 # shared localedata objects.
diff --git a/libio/tst-fputws.c b/libio/tst-fputws.c
new file mode 100644 (file)
index 0000000..09f53df
--- /dev/null
@@ -0,0 +1,39 @@
+/* Test that we can write a multibyte character to an unbuffered stream.
+   Copyright (C) 2014 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 <locale.h>
+#include <stdio.h>
+#include <wchar.h>
+
+static int
+do_test (void)
+{
+  const wchar_t str[] = L"\xbe\n";
+
+  setlocale (LC_ALL, "en_US.UTF-8");
+  setvbuf (stdout, NULL, _IONBF, 0);
+
+  if (fputws (str, stdout) < 0)
+    return 1;
+
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+
+#include <test-skeleton.c>
index c5ec5f7a278dd08064cbc4162ab758217281f0b7..6a088b1c1559058a723a70cb3b1aa21e87ae3bd0 100644 (file)
@@ -75,17 +75,32 @@ _IO_wdo_write (fp, data, to_do)
        {
          enum __codecvt_result result;
          const wchar_t *new_data;
+         char mb_buf[MB_LEN_MAX];
+         char *write_base, *write_ptr, *buf_end;
+
+         if (fp->_IO_write_ptr - fp->_IO_write_base < sizeof (mb_buf))
+           {
+             /* Make sure we have room for at least one multibyte
+                character.  */
+             write_ptr = write_base = mb_buf;
+             buf_end = mb_buf + sizeof (mb_buf);
+           }
+         else
+           {
+             write_ptr = fp->_IO_write_ptr;
+             write_base = fp->_IO_write_base;
+             buf_end = fp->_IO_buf_end;
+           }
 
          /* Now convert from the internal format into the external buffer.  */
          result = (*cc->__codecvt_do_out) (cc, &fp->_wide_data->_IO_state,
                                            data, data + to_do, &new_data,
-                                           fp->_IO_write_ptr,
-                                           fp->_IO_buf_end,
-                                           &fp->_IO_write_ptr);
+                                           write_ptr,
+                                           buf_end,
+                                           &write_ptr);
 
          /* Write out what we produced so far.  */
-         if (_IO_new_do_write (fp, fp->_IO_write_base,
-                               fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
+         if (_IO_new_do_write (fp, write_base, write_ptr - write_base) == EOF)
            /* Something went wrong.  */
            return WEOF;
 
This page took 0.11888 seconds and 5 git commands to generate.