Bug 24051 - puts and putchar output to _IO_stdout instead of stdout
Summary: puts and putchar output to _IO_stdout instead of stdout
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: 2.30
Assignee: Paul Pluzhnikov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-01-01 01:10 UTC by Paul Pluzhnikov
Modified: 2020-11-02 07:44 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Pluzhnikov 2019-01-01 01:10:23 UTC
Reported in 2014 here: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=761300
Rediscovered again: https://stackoverflow.com/a/53981648/50617

Trivial test:

#include <stdio.h>

int main (int argc, char *argv[])
{
  stdout = fopen ("/dev/null", "w");

  printf ("argc = %d\n", argc);  // Goes to /dev/null as expected

  putchar ('a');  // Goes to terminal (bug)
  puts("bcd");    // Likewise

  return 0;
}


gcc -g f.c && ./a.out
abcd

Verified this is still a problem with current trunk (7628a1b05adb1e4c6857b87c6f8b71a1d0b1d72c).
Comment 1 Paul Pluzhnikov 2019-01-01 03:34:03 UTC
The same problem exists for stdin / getchar() and gets().
Comment 2 Florian Weimer 2019-01-02 12:16:18 UTC
Paul, do you plan to work on this?

Please also fix the wide versions at the same time if possible.  Thanks.
Comment 3 Sourceware Commits 2019-01-31 22:20:37 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  5f10701fdc7a4708101f7931c15b7146c4f06690 (commit)
      from  583dd860d5b833037175247230a328f0050dbfe9 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=5f10701fdc7a4708101f7931c15b7146c4f06690

commit 5f10701fdc7a4708101f7931c15b7146c4f06690
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
Date:   Mon Dec 31 19:14:28 2018 -0800

    libio: use stdout in puts and putchar, etc [BZ #24051].
    
    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.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog           |   13 ++++++++
 libio/Makefile      |    2 +-
 libio/fileops.c     |    8 ++--
 libio/ioputs.c      |   12 ++++----
 libio/putchar.c     |    6 ++--
 libio/putchar_u.c   |    4 +-
 libio/putwchar.c    |    6 ++--
 libio/putwchar_u.c  |    4 +-
 libio/tst-bz24051.c |   81 +++++++++++++++++++++++++++++++++++++++++++++++++++
 libio/wfileops.c    |    8 ++--
 10 files changed, 119 insertions(+), 25 deletions(-)
 create mode 100644 libio/tst-bz24051.c
Comment 4 Florian Weimer 2019-02-01 11:58:56 UTC
Fixed in glibc 2.30.