zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 6a1021fb7d14604b4f66b2c992f47eadcc812989 (tree)
parent 077b003c50065a8b193dcad793ca5c4474222af6
Author: Ryan Liptak <squeek502@hotmail.com>
Date:   Fri, 16 Dec 2022 19:33:06 -0800

ChildProcess.spawnWindows: Fix PATH search when the ext is in the command

For example, if the command is specified as `something.exe`, the retry will now try:

```
C:\some\path\something.exe
C:\some\path\something.exe.COM
C:\some\path\something.exe.EXE
C:\some\path\something.exe.BAT
... etc ...
```

whereas before it would only try the versions with an added extension from `PATHEXT`, which would cause the retry to fail on things that it should find.

Diffstat:
Mlib/std/child_process.zig | 16+++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig @@ -995,12 +995,22 @@ pub const ChildProcess = struct { const path_no_ext = try fs.path.join(self.allocator, &[_][]const u8{ search_path, app_name }); defer self.allocator.free(path_no_ext); + const path_no_ext_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, path_no_ext); + defer self.allocator.free(path_no_ext_w); + + if (windowsCreateProcess(path_no_ext_w.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) |_| { + break :retry; + } else |err| switch (err) { + error.FileNotFound, error.AccessDenied => {}, + else => return err, + } + var ext_it = mem.tokenize(u8, PATHEXT, ";"); while (ext_it.next()) |app_ext| { - const joined_path = try mem.concat(self.allocator, u8, &[_][]const u8{ path_no_ext, app_ext }); - defer self.allocator.free(joined_path); + const ext_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, app_ext); + defer self.allocator.free(ext_w); - const joined_path_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, joined_path); + const joined_path_w = try std.mem.concatWithSentinel(self.allocator, u16, &.{ path_no_ext_w, ext_w }, 0); defer self.allocator.free(joined_path_w); if (windowsCreateProcess(joined_path_w.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) |_| {