Skip to content

Commit cf2034b

Browse files
committed
Processing (Windows): redirect unused std handles to NUL to avoid potential errors
1 parent f7db03b commit cf2034b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/common/io/io_windows.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ bool ffSuppressIO(bool suppress)
175175
static bool init = false;
176176
static HANDLE hOrigOut = INVALID_HANDLE_VALUE;
177177
static HANDLE hOrigErr = INVALID_HANDLE_VALUE;
178-
static HANDLE hNullFile = INVALID_HANDLE_VALUE;
178+
HANDLE hNullFile = ffGetNullFD();
179179
static int fOrigOut = -1;
180180
static int fOrigErr = -1;
181181
static int fNullFile = -1;
@@ -187,7 +187,6 @@ bool ffSuppressIO(bool suppress)
187187

188188
hOrigOut = GetStdHandle(STD_OUTPUT_HANDLE);
189189
hOrigErr = GetStdHandle(STD_ERROR_HANDLE);
190-
hNullFile = CreateFileW(L"NUL", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, NULL);
191190
fOrigOut = _dup(STDOUT_FILENO);
192191
fOrigErr = _dup(STDERR_FILENO);
193192
fNullFile = _open_osfhandle((intptr_t) hNullFile, 0);
@@ -352,3 +351,23 @@ const char* ffGetTerminalResponse(const char* request, int nParams, const char*
352351

353352
return NULL;
354353
}
354+
355+
FFNativeFD ffGetNullFD(void)
356+
{
357+
static FFNativeFD hNullFile = INVALID_HANDLE_VALUE;
358+
if (hNullFile != INVALID_HANDLE_VALUE)
359+
return hNullFile;
360+
hNullFile = CreateFileW(
361+
L"NUL",
362+
GENERIC_READ | GENERIC_WRITE,
363+
FILE_SHARE_WRITE,
364+
0,
365+
OPEN_EXISTING,
366+
0,
367+
&(SECURITY_ATTRIBUTES){
368+
.nLength = sizeof(SECURITY_ATTRIBUTES),
369+
.lpSecurityDescriptor = NULL,
370+
.bInheritHandle = TRUE,
371+
});
372+
return hNullFile;
373+
}

src/common/processing_windows.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
8181
if (hChildPipeWrite == INVALID_HANDLE_VALUE)
8282
return "CreateFileW(L\"\\\\.\\pipe\\FASTFETCH-$(PID)\") failed";
8383

84-
PROCESS_INFORMATION piProcInfo = {0};
84+
PROCESS_INFORMATION piProcInfo = {};
8585

8686
BOOL success;
8787

@@ -91,9 +91,15 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
9191
.dwFlags = STARTF_USESTDHANDLES,
9292
};
9393
if (useStdErr)
94+
{
95+
siStartInfo.hStdOutput = ffGetNullFD();
9496
siStartInfo.hStdError = hChildPipeWrite;
97+
}
9598
else
99+
{
96100
siStartInfo.hStdOutput = hChildPipeWrite;
101+
siStartInfo.hStdError = ffGetNullFD();
102+
}
97103

98104
FF_STRBUF_AUTO_DESTROY cmdline = ffStrbufCreate();
99105
argvToCmdline(argv, &cmdline);

0 commit comments

Comments
 (0)