From cf2034b93e73f791a7e2335c074a03eca0455a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 18 Jul 2025 10:39:11 +0800 Subject: [PATCH] Processing (Windows): redirect unused std handles to `NUL` to avoid potential errors --- src/common/io/io_windows.c | 23 +++++++++++++++++++++-- src/common/processing_windows.c | 8 +++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/common/io/io_windows.c b/src/common/io/io_windows.c index fa3057e76..419a29929 100644 --- a/src/common/io/io_windows.c +++ b/src/common/io/io_windows.c @@ -175,7 +175,7 @@ bool ffSuppressIO(bool suppress) static bool init = false; static HANDLE hOrigOut = INVALID_HANDLE_VALUE; static HANDLE hOrigErr = INVALID_HANDLE_VALUE; - static HANDLE hNullFile = INVALID_HANDLE_VALUE; + HANDLE hNullFile = ffGetNullFD(); static int fOrigOut = -1; static int fOrigErr = -1; static int fNullFile = -1; @@ -187,7 +187,6 @@ bool ffSuppressIO(bool suppress) hOrigOut = GetStdHandle(STD_OUTPUT_HANDLE); hOrigErr = GetStdHandle(STD_ERROR_HANDLE); - hNullFile = CreateFileW(L"NUL", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, NULL); fOrigOut = _dup(STDOUT_FILENO); fOrigErr = _dup(STDERR_FILENO); fNullFile = _open_osfhandle((intptr_t) hNullFile, 0); @@ -352,3 +351,23 @@ const char* ffGetTerminalResponse(const char* request, int nParams, const char* return NULL; } + +FFNativeFD ffGetNullFD(void) +{ + static FFNativeFD hNullFile = INVALID_HANDLE_VALUE; + if (hNullFile != INVALID_HANDLE_VALUE) + return hNullFile; + hNullFile = CreateFileW( + L"NUL", + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_WRITE, + 0, + OPEN_EXISTING, + 0, + &(SECURITY_ATTRIBUTES){ + .nLength = sizeof(SECURITY_ATTRIBUTES), + .lpSecurityDescriptor = NULL, + .bInheritHandle = TRUE, + }); + return hNullFile; +} diff --git a/src/common/processing_windows.c b/src/common/processing_windows.c index 3ec86583e..c8f28d87e 100644 --- a/src/common/processing_windows.c +++ b/src/common/processing_windows.c @@ -81,7 +81,7 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use if (hChildPipeWrite == INVALID_HANDLE_VALUE) return "CreateFileW(L\"\\\\.\\pipe\\FASTFETCH-$(PID)\") failed"; - PROCESS_INFORMATION piProcInfo = {0}; + PROCESS_INFORMATION piProcInfo = {}; BOOL success; @@ -91,9 +91,15 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use .dwFlags = STARTF_USESTDHANDLES, }; if (useStdErr) + { + siStartInfo.hStdOutput = ffGetNullFD(); siStartInfo.hStdError = hChildPipeWrite; + } else + { siStartInfo.hStdOutput = hChildPipeWrite; + siStartInfo.hStdError = ffGetNullFD(); + } FF_STRBUF_AUTO_DESTROY cmdline = ffStrbufCreate(); argvToCmdline(argv, &cmdline);