Skip to content

Commit ddee2dd

Browse files
author
Jan Philipp Hafer
committed
windows: fix leak by interface change
Pipes should be created with inheritance disabled and enabling should be explicit, not behind developer's back.
1 parent 7cb6146 commit ddee2dd

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/std/child_process.zig

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ pub const ChildProcess = struct {
821821
fn spawnWindows(self: *ChildProcess) SpawnError!void {
822822
const saAttr = windows.SECURITY_ATTRIBUTES{
823823
.nLength = @sizeOf(windows.SECURITY_ATTRIBUTES),
824-
.bInheritHandle = windows.TRUE,
824+
.bInheritHandle = windows.FALSE,
825825
.lpSecurityDescriptor = null,
826826
};
827827

@@ -883,6 +883,11 @@ pub const ChildProcess = struct {
883883
&tmp_hChildStd_Wr,
884884
&saAttr,
885885
);
886+
errdefer {
887+
os.close(tmp_hChildStd_Rd);
888+
os.close(tmp_hChildStd_Wr);
889+
}
890+
try windows.SetHandleInformation(tmp_hChildStd_Wr, windows.HANDLE_FLAG_INHERIT, 1);
886891
g_hChildStd_OUT_Rd = tmp_hChildStd_Rd;
887892
g_hChildStd_OUT_Wr = tmp_hChildStd_Wr;
888893
},
@@ -909,6 +914,11 @@ pub const ChildProcess = struct {
909914
&tmp_hChildStd_Wr,
910915
&saAttr,
911916
);
917+
errdefer {
918+
os.close(tmp_hChildStd_Rd);
919+
os.close(tmp_hChildStd_Wr);
920+
}
921+
try windows.SetHandleInformation(tmp_hChildStd_Wr, windows.HANDLE_FLAG_INHERIT, 1);
912922
g_hChildStd_ERR_Rd = tmp_hChildStd_Rd;
913923
g_hChildStd_ERR_Wr = tmp_hChildStd_Wr;
914924
},
@@ -1495,7 +1505,7 @@ fn windowsMakePipeIn(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *const w
14951505
var wr_h: windows.HANDLE = undefined;
14961506
try windows.CreatePipe(&rd_h, &wr_h, sattr);
14971507
errdefer windowsDestroyPipe(rd_h, wr_h);
1498-
try windows.SetHandleInformation(wr_h, windows.HANDLE_FLAG_INHERIT, 0);
1508+
try windows.SetHandleInformation(rd_h, windows.HANDLE_FLAG_INHERIT, 1);
14991509
rd.* = rd_h;
15001510
wr.* = wr_h;
15011511
}
@@ -1504,6 +1514,8 @@ var pipe_name_counter = std.atomic.Atomic(u32).init(1);
15041514

15051515
/// To enable/disable inheritance parent and child process, use
15061516
/// os.enableInheritance() and os.disableInheritance() on the handle.
1517+
/// convention: sattr uses bInheritHandle = windows.FALSE and only used pipe end
1518+
/// is enabled.
15071519
pub fn windowsMakeAsyncPipe(
15081520
rd: *windows.HANDLE,
15091521
wr: *windows.HANDLE,
@@ -1561,7 +1573,6 @@ pub fn windowsMakeAsyncPipe(
15611573
else => |err| return windows.unexpectedError(err),
15621574
}
15631575
}
1564-
errdefer os.close(write_handle);
15651576

15661577
rd.* = read_handle;
15671578
wr.* = write_handle;

0 commit comments

Comments
 (0)