Browse Source

fix(Process): CloseHandle() on an null/invalid handle will throw an exception in the debugger

Günter Obiltschnig 1 year ago
parent
commit
a25cdac549
1 changed files with 15 additions and 3 deletions
  1. 15 3
      Foundation/src/Process_WIN32U.cpp

+ 15 - 3
Foundation/src/Process_WIN32U.cpp

@@ -280,7 +280,11 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
 	{
 		startupInfo.hStdInput = 0;
 	}
-	if (options & PROCESS_CLOSE_STDIN) CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
+	if (options & PROCESS_CLOSE_STDIN)
+	{
+		HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
+		if (hStdIn) CloseHandle(hStdIn);
+	}
 
 	// outPipe may be the same as errPipe, so we duplicate first and close later.
 	if (outPipe)
@@ -312,9 +316,17 @@ ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const Arg
 		startupInfo.hStdError = 0;
 	}
 	if (outPipe) outPipe->close(Pipe::CLOSE_WRITE);
-	if (options & PROCESS_CLOSE_STDOUT) CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE));
+	if (options & PROCESS_CLOSE_STDOUT) 
+	{
+		HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
+		if (hStdOut) CloseHandle(hStdOut);
+	}
 	if (errPipe) errPipe->close(Pipe::CLOSE_WRITE);
-	if (options & PROCESS_CLOSE_STDERR) CloseHandle(GetStdHandle(STD_ERROR_HANDLE));
+	if (options & PROCESS_CLOSE_STDERR) 
+	{
+		HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
+		if (hStdErr) CloseHandle(hStdErr);
+	}
 
 	if (mustInheritHandles)
 	{