From c95d2b6372facd3369bd50136f6c216ed384467b Mon Sep 17 00:00:00 2001 From: riastradh Date: Mon, 20 May 2024 11:20:53 +0000 Subject: [PATCH] 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 --- .../gpl3/gcc/dist/libstdc++-v3/config/io/basic_file_stdio.cc | 4 ++-- tests/lib/libstdc++/t_sync_with_stdio.sh | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/external/gpl3/gcc/dist/libstdc++-v3/config/io/basic_file_stdio.cc b/external/gpl3/gcc/dist/libstdc++-v3/config/io/basic_file_stdio.cc index 09fdd750b807..5717924944bc 100644 --- a/external/gpl3/gcc/dist/libstdc++-v3/config/io/basic_file_stdio.cc +++ b/external/gpl3/gcc/dist/libstdc++-v3/config/io/basic_file_stdio.cc @@ -204,7 +204,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { this->close(); } __basic_file* - __basic_file::sys_open(__c_file* __file, ios_base::openmode) + __basic_file::sys_open(__c_file* __file, ios_base::openmode __mode) { __basic_file* __ret = NULL; 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. errno = 0; do - __err = fflush(__file); + __err = (__mode == std::ios_base::in ? 0 : fflush(__file)); while (__err && errno == EINTR); errno = __save_errno; if (!__err) diff --git a/tests/lib/libstdc++/t_sync_with_stdio.sh b/tests/lib/libstdc++/t_sync_with_stdio.sh index 9c1dfaf7018d..fe76c49b988f 100644 --- a/tests/lib/libstdc++/t_sync_with_stdio.sh +++ b/tests/lib/libstdc++/t_sync_with_stdio.sh @@ -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. # All rights reserved. @@ -32,7 +32,6 @@ cin_nosync_head() cin_nosync_body() { 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