Browse Source

Set pipe handles in right place (avoids leak) + add comment

Jordan Russell 1 year ago
parent
commit
30e13e5454
1 changed files with 9 additions and 3 deletions
  1. 9 3
      Projects/Src/CmnFunc2.pas

+ 9 - 3
Projects/Src/CmnFunc2.pas

@@ -1615,15 +1615,21 @@ constructor TCreateProcessOutputReader.Create(const ALogProc: TLogProc;
 
   procedure PipeCreate(var Read, Write: THandle; SecurityAttr: TSecurityAttributes);
   begin
+    { CreatePipe docs say no assumptions should be made about the output
+      parameter contents (the two handles) when it fails. So specify local
+      variables for the output parameters, and only copy the handles into
+      the "var" parameters when CreatePipe is successful. That way, if it
+      does fail, the "var" parameters will still have their original 0
+      values (which is important because the destructor closes all
+      non-zero handles). }
     var TempReadPipe, TempWritePipe: THandle;
     if not CreatePipe(TempReadPipe, TempWritePipe, @SecurityAttr, 0) then
       raise Exception.CreateFmt('Output redirection error: CreatePipe failed (%d)', [GetLastError]);
+    Read := TempReadPipe;
+    Write := TempWritePipe;
 
     if not SetHandleInformation(TempReadPipe, HANDLE_FLAG_INHERIT, 0) then
       raise Exception.CreateFmt('Output redirection error: SetHandleInformation failed (%d)', [GetLastError]);
-
-    Read := TempReadPipe;
-    Write := TempWritePipe;
   end;
 
 begin