]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
libio: use stdout in puts and putchar, etc [BZ #24051].
authorPaul Pluzhnikov <ppluzhnikov@google.com>
Tue, 1 Jan 2019 03:14:28 +0000 (19:14 -0800)
committerPaul Pluzhnikov <ppluzhnikov@google.com>
Thu, 31 Jan 2019 22:07:45 +0000 (14:07 -0800)
GLIBC explicitly allows one to assign a new FILE pointer to stdout and
other standard streams.  printf and wprintf were honouring assignment to
stdout and using the new value, but puts, putchar, and wide char variants
did not.

The stdout part is fixed here.  The stdin part will be fixed in a followup.

ChangeLog
libio/Makefile
libio/fileops.c
libio/ioputs.c
libio/putchar.c
libio/putchar_u.c
libio/putwchar.c
libio/putwchar_u.c
libio/tst-bz24051.c [new file with mode: 0644]
libio/wfileops.c

index 62d732e6e7f821ca80d00aa2bf64637ece7d849d..93201b76a26b4108cbf146c993e551b7d10c2e82 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2019-01-31  Paul Pluzhnikov  <ppluzhnikov@google.com>
+
+       [BZ #24051]
+       * libio/ioputs.c (_IO_puts): Use stdout instead of _IO_stdout.
+       * libio/fileops.c (_IO_new_file_underflow): Likewise
+       * libio/wfileops.c (_IO_wfile_underflow): Likewise
+       * libio/putchar.c (putchar): Likewise.
+       * libio/putchar_u.c (putchar_unlocked): Likewise.
+       * libio/putwchar.c (putchar): Likewise.
+       * libio/putwchar_u.c (putwchar_unlocked): Likewise.
+       * libio/tst-bz24051.c: New test.
+       * libio/Makefile (tests): Add tst-bz24051
+
 2019-01-31  Paul Eggert  <eggert@cs.ucla.edu>
 
        regex: fix read overrun [BZ #24114]
index 5bee83e55c08094b15ebc42adf8e472cf2ddab7f..dbeba88fc088592d20e26bf9a73d1d677871e0e7 100644 (file)
@@ -65,7 +65,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
        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-fputws tst-bz22415 tst-fgetc-after-eof \
-       tst-sprintf-ub tst-sprintf-chk-ub
+       tst-sprintf-ub tst-sprintf-chk-ub tst-bz24051
 
 tests-internal = tst-vtables tst-vtables-interposed tst-readline
 
index 43e33820e3bba90109f3fafb7721f49b4bb60adc..d2070a856e5c689b829b60dc5ac50cbd484e8e6e 100644 (file)
@@ -501,13 +501,13 @@ _IO_new_file_underflow (FILE *fp)
         traditional Unix systems did this for stdout.  stderr better
         not be line buffered.  So we do just that here
         explicitly.  --drepper */
-      _IO_acquire_lock (_IO_stdout);
+      _IO_acquire_lock (stdout);
 
-      if ((_IO_stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
+      if ((stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
          == (_IO_LINKED | _IO_LINE_BUF))
-       _IO_OVERFLOW (_IO_stdout, EOF);
+       _IO_OVERFLOW (stdout, EOF);
 
-      _IO_release_lock (_IO_stdout);
+      _IO_release_lock (stdout);
     }
 
   _IO_switch_to_get_mode (fp);
index 04ae323c6d21d8d916bda830857daae871788b6a..319e551de589d9659c1a71b0dd0fa3479eb5c1c7 100644 (file)
@@ -33,15 +33,15 @@ _IO_puts (const char *str)
 {
   int result = EOF;
   size_t len = strlen (str);
-  _IO_acquire_lock (_IO_stdout);
+  _IO_acquire_lock (stdout);
 
-  if ((_IO_vtable_offset (_IO_stdout) != 0
-       || _IO_fwide (_IO_stdout, -1) == -1)
-      && _IO_sputn (_IO_stdout, str, len) == len
-      && _IO_putc_unlocked ('\n', _IO_stdout) != EOF)
+  if ((_IO_vtable_offset (stdout) != 0
+       || _IO_fwide (stdout, -1) == -1)
+      && _IO_sputn (stdout, str, len) == len
+      && _IO_putc_unlocked ('\n', stdout) != EOF)
     result = MIN (INT_MAX, len + 1);
 
-  _IO_release_lock (_IO_stdout);
+  _IO_release_lock (stdout);
   return result;
 }
 
index 665f46685a55c4a75c5491d7a2f5f7f33f26d54c..a3f4200cdb59fd02de99ad610b21490a983c8592 100644 (file)
@@ -24,9 +24,9 @@ int
 putchar (int c)
 {
   int result;
-  _IO_acquire_lock (_IO_stdout);
-  result = _IO_putc_unlocked (c, _IO_stdout);
-  _IO_release_lock (_IO_stdout);
+  _IO_acquire_lock (stdout);
+  result = _IO_putc_unlocked (c, stdout);
+  _IO_release_lock (stdout);
   return result;
 }
 
index 37d03ad364b770b0e0ae27d4643d9b2539836d08..1eebf0fc8f5ef3f46f4af4f2b249571f9f330ac8 100644 (file)
@@ -23,6 +23,6 @@
 int
 putchar_unlocked (int c)
 {
-  CHECK_FILE (_IO_stdout, EOF);
-  return _IO_putc_unlocked (c, _IO_stdout);
+  CHECK_FILE (stdout, EOF);
+  return _IO_putc_unlocked (c, stdout);
 }
index 8d6b6a4df01f9aed8c0f0d7dd4ca55e0b0ac8c28..1f5c4176f85b65ca930a7651eec08058fca0043b 100644 (file)
@@ -22,8 +22,8 @@ wint_t
 putwchar (wchar_t wc)
 {
   wint_t result;
-  _IO_acquire_lock (_IO_stdout);
-  result = _IO_putwc_unlocked (wc, _IO_stdout);
-  _IO_release_lock (_IO_stdout);
+  _IO_acquire_lock (stdout);
+  result = _IO_putwc_unlocked (wc, stdout);
+  _IO_release_lock (stdout);
   return result;
 }
index cfb46fc253d047c29731c81863f8bd06157dda27..d943220031b7fee4407a0659f491dda9b60877af 100644 (file)
@@ -21,6 +21,6 @@
 wint_t
 putwchar_unlocked (wchar_t wc)
 {
-  CHECK_FILE (_IO_stdout, WEOF);
-  return _IO_putwc_unlocked (wc, _IO_stdout);
+  CHECK_FILE (stdout, WEOF);
+  return _IO_putwc_unlocked (wc, stdout);
 }
diff --git a/libio/tst-bz24051.c b/libio/tst-bz24051.c
new file mode 100644 (file)
index 0000000..cf0dc80
--- /dev/null
@@ -0,0 +1,81 @@
+/* Test that assigning to stdout redirects puts, putchar, etc (BZ#24051)
+   Copyright (C) 2019 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/>.  */
+
+
+/* Prevent putchar -> _IO_putc inline expansion.  */
+#define __NO_INLINE__
+#pragma GCC optimize("O0")
+
+#include <stdio.h>
+#include <string.h>
+#include <wchar.h>
+
+#include <array_length.h>
+#include <support/check.h>
+#include <support/temp_file.h>
+#include <support/test-driver.h>
+
+#undef putchar
+#undef putwchar
+
+static int
+do_test_narrow (void)
+{
+  char buf[100];
+  int fd = create_temp_file ("tst-bz24051", NULL);
+  stdout = fdopen (fd, "w+");
+  TEST_VERIFY_EXIT (stdout != NULL);
+
+  printf ("ab%s", "cd");
+  putchar ('e');
+  putchar_unlocked ('f');
+  puts ("ghi");
+
+  rewind (stdout);
+  TEST_VERIFY_EXIT (fgets (buf, sizeof (buf), stdout) != NULL);
+  TEST_VERIFY (strcmp (buf, "abcdefghi\n") == 0);
+
+  return 0;
+}
+
+static int
+do_test_wide (void)
+{
+  wchar_t buf[100];
+  int fd = create_temp_file ("tst-bz24051w", NULL);
+  stdout = fdopen (fd, "w+");
+  TEST_VERIFY_EXIT (stdout != NULL);
+
+  wprintf (L"ab%ls", L"cd");
+  putwchar (L'e');
+  putwchar_unlocked (L'f');
+
+  rewind (stdout);
+  TEST_VERIFY_EXIT (fgetws (buf, array_length (buf), stdout) != NULL);
+  TEST_VERIFY (wcscmp (buf, L"abcdef") == 0);
+
+  return 0;
+}
+
+static int
+do_test (void)
+{
+  return do_test_narrow () + do_test_wide ();
+}
+
+#include <support/test-driver.c>
index 78f20486e50c87b882e2214cba7260f787fbc9f2..03676437035b15e51dcf88c19ac24f83f308caab 100644 (file)
@@ -208,13 +208,13 @@ _IO_wfile_underflow (FILE *fp)
         traditional Unix systems did this for stdout.  stderr better
         not be line buffered.  So we do just that here
         explicitly.  --drepper */
-      _IO_acquire_lock (_IO_stdout);
+      _IO_acquire_lock (stdout);
 
-      if ((_IO_stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
+      if ((stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
          == (_IO_LINKED | _IO_LINE_BUF))
-       _IO_OVERFLOW (_IO_stdout, EOF);
+       _IO_OVERFLOW (stdout, EOF);
 
-      _IO_release_lock (_IO_stdout);
+      _IO_release_lock (stdout);
     }
 
   _IO_switch_to_get_mode (fp);