libstdc++: Don't try to fflush stdin.

It doesn't work.  It's undefined behaviour.  On NetBSD, it will fail
with EBADF, if fd 0 isn't open for write, or if fd 0 is open for
write, it will write heap garbage to fd 0.

   If stream points to an output stream or an update stream in which
   the most recent operation was not input, the fflush function causes
   any unwritten data for that stream to be delivered to the host
   environment to be written to the file; otherwise, the behavior is
   undefined.

   (ISO C11 and ISO C17, Sec. 7.21.5.2 `The fflush function')

PR lib/58206
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114879
This commit is contained in:
riastradh 2024-05-20 11:20:53 +00:00
parent 5d83fcdc53
commit c95d2b6372
2 changed files with 3 additions and 4 deletions

View File

@ -204,7 +204,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ this->close(); } { this->close(); }
__basic_file<char>* __basic_file<char>*
__basic_file<char>::sys_open(__c_file* __file, ios_base::openmode) __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode __mode)
{ {
__basic_file* __ret = NULL; __basic_file* __ret = NULL;
if (!this->is_open() && __file) if (!this->is_open() && __file)
@ -213,7 +213,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// POSIX guarantees that fflush sets errno on error, but C doesn't. // POSIX guarantees that fflush sets errno on error, but C doesn't.
errno = 0; errno = 0;
do do
__err = fflush(__file); __err = (__mode == std::ios_base::in ? 0 : fflush(__file));
while (__err && errno == EINTR); while (__err && errno == EINTR);
errno = __save_errno; errno = __save_errno;
if (!__err) if (!__err)

View File

@ -1,4 +1,4 @@
# $NetBSD: t_sync_with_stdio.sh,v 1.1 2024/04/28 01:21:27 riastradh Exp $ # $NetBSD: t_sync_with_stdio.sh,v 1.2 2024/05/20 11:20:53 riastradh Exp $
# #
# Copyright (c) 2024 The NetBSD Foundation, Inc. # Copyright (c) 2024 The NetBSD Foundation, Inc.
# All rights reserved. # All rights reserved.
@ -32,7 +32,6 @@ cin_nosync_head()
cin_nosync_body() cin_nosync_body()
{ {
echo hello >in echo hello >in
atf_expect_fail "PR lib/58206: sync_with_stdio breaks reads from cin"
atf_check -o inline:'6\n' "$(atf_get_srcdir)"/h_cin_nosync <in atf_check -o inline:'6\n' "$(atf_get_srcdir)"/h_cin_nosync <in
} }