From 2f51894279f114cfb50dd18fc9b8338b00d2ff80 Mon Sep 17 00:00:00 2001 From: Kien Nguyen Date: Wed, 7 Aug 2024 10:39:38 -0700 Subject: [PATCH] Use SetHandleInformation to set NOINHERIT in UCRT64 * src/w32.c (init_ntproc) [_UCRT]: Use SetHandleInformation to prevent standard handles from being inherited, instead of relying on MSVCRT-only behavior. For the details of the problem, see https://lists.gnu.org/archive/html/emacs-devel/2024-07/msg01129.html. Copyright-paperwork-exempt: yes (cherry picked from commit de7de561e1e3a973581b6993b3cc0074ff0bf91c) --- src/w32.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/w32.c b/src/w32.c index 31ffa301c2f..0c1291f1094 100644 --- a/src/w32.c +++ b/src/w32.c @@ -10480,6 +10480,16 @@ init_ntproc (int dumping) /* Initial preparation for subprocess support: replace our standard handles with non-inheritable versions. */ { + +#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 HANDLE parent; HANDLE stdin_save = INVALID_HANDLE_VALUE; HANDLE stdout_save = INVALID_HANDLE_VALUE; @@ -10534,6 +10544,7 @@ init_ntproc (int dumping) else _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY); _fdopen (2, "w"); +#endif } /* unfortunately, atexit depends on implementation of malloc */ -- 2.39.2