From e523f86fed4465670ffef257ea15a9c36058b1b5 Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Fri, 9 Aug 2024 14:00:34 +0300 Subject: [PATCH] ; * src/w32.c (init_ntproc): Fix formatting conventions in last change. (cherry picked from commit 944e45db53cb173c5eadd4794081c133e8649d67) --- src/w32.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/w32.c b/src/w32.c index 0c1291f1094..cec15606bf2 100644 --- a/src/w32.c +++ b/src/w32.c @@ -10482,14 +10482,19 @@ init_ntproc (int dumping) { #ifdef _UCRT - /* For UCRT, the _fdopen will try to find free stream from - _IOB_ENTRIES (= 3), thus we can't reopen the standard handles - with it. Using SetHandleInformation to make the handle not - inheritable to child process is a better way. */ - SetHandleInformation (GetStdHandle(STD_INPUT_HANDLE), HANDLE_FLAG_INHERIT, 0); - SetHandleInformation (GetStdHandle(STD_OUTPUT_HANDLE), HANDLE_FLAG_INHERIT, 0); - SetHandleInformation (GetStdHandle(STD_ERROR_HANDLE), HANDLE_FLAG_INHERIT, 0); -#else + /* The non-UCRT code below relies on MSVCRT-only behavior, whereby + _fdopen reuses the first unused FILE slot, whereas UCRT skips the + first 3 slots, which correspond to stdin/stdout/stderr. That + makes it impossible in the UCRT build to open these 3 streams + once they are closed. So we use SetHandleInformation instead, + which is available on all versions of Windows that have UCRT. */ + SetHandleInformation (GetStdHandle(STD_INPUT_HANDLE), + HANDLE_FLAG_INHERIT, 0); + SetHandleInformation (GetStdHandle(STD_OUTPUT_HANDLE), + HANDLE_FLAG_INHERIT, 0); + SetHandleInformation (GetStdHandle(STD_ERROR_HANDLE), + HANDLE_FLAG_INHERIT, 0); +#else /* !_UCRT */ HANDLE parent; HANDLE stdin_save = INVALID_HANDLE_VALUE; HANDLE stdout_save = INVALID_HANDLE_VALUE; @@ -10497,8 +10502,8 @@ init_ntproc (int dumping) parent = GetCurrentProcess (); - /* ignore errors when duplicating and closing; typically the - handles will be invalid when running as a gui program. */ + /* Ignore errors when duplicating and closing; typically the + handles will be invalid when running as a gui program. */ DuplicateHandle (parent, GetStdHandle (STD_INPUT_HANDLE), parent, @@ -10544,7 +10549,7 @@ init_ntproc (int dumping) else _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY); _fdopen (2, "w"); -#endif +#endif /* !_UCRT */ } /* unfortunately, atexit depends on implementation of malloc */ -- 2.39.2