Skip to content

Return FileNotFound when CreateProcessW is called with a missing path #23567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2025

Conversation

phatchman
Copy link
Contributor

@phatchman phatchman commented Apr 14, 2025

Fixes #19023

CreateProcessW currently returns error.Unexpected when the lpCurrentDirectory value is a directory that does not exist.
With this PR, CreateProcessW returns error.FileNotFound in those cases.

Directory not existing and directory invalid cannot be distinguished. CreateProcessW returns [ERROR_DIRECTORY - 267 (0x10B) - The directory name is invalid.] in both cases.

Can be tested with the following test.

test "test CreateProcessW with invalid path" {
    const allocator = std.testing.allocator;
    const application_name = try std.unicode.utf8ToUtf16LeAllocZ(allocator, "c:\\windows\\explorer.exe");
    defer allocator.free(application_name);
    const command_line = try std.unicode.utf8ToUtf16LeAllocZ(allocator, "");
    defer allocator.free(command_line);
    const startup_directory = try std.unicode.utf8ToUtf16LeAllocZ(allocator, "c:\\directory_that_does_not_exist");
    defer allocator.free(startup_directory);

    var startup_info = std.mem.zeroes(std.os.windows.STARTUPINFOW);
    var process_info = std.mem.zeroes(std.os.windows.PROCESS_INFORMATION);
    const inherit: std.os.windows.BOOL = 0;
    const flags = std.mem.zeroes(std.os.windows.CreateProcessFlags);

    try std.testing.expectError(error.FileNotFound, std.os.windows.CreateProcessW(
        application_name,
        command_line,
        null,
        null,
        inherit,
        flags,
        null,
        startup_directory,
        &startup_info,
        &process_info,
    ));
}
const std = @import("std");

@alexrp alexrp requested a review from squeek502 April 14, 2025 13:24
@squeek502
Copy link
Collaborator

squeek502 commented Apr 14, 2025

Nice , thanks for the fix! Would be good to add a test for this to the windows_spawn standalone test

Something like:

try std.testing.expectError(error.FileNotFound, testExecWithCwd(allocator, "hello.exe", "missing_dir", ""));

after these lines

@phatchman
Copy link
Contributor Author

phatchman commented Apr 15, 2025

Test added and confirmed that all standalone tests pass.

I'm new to making PRs, so please let me know if I've done this right or whether I should have made a single commit with all the changes?

@alexrp alexrp enabled auto-merge (squash) April 15, 2025 13:13
@alexrp
Copy link
Member

alexrp commented Apr 15, 2025

I'm new to making PRs, so please let me know if I've done this right or whether I should have made a single commit with all the changes?

For future reference, it's generally best to add the test in the same commit that makes the change being tested. But since this is a fairly small/focused PR, we can just squash the two commits when merging, so no big deal.

@alexrp alexrp merged commit ae38fc6 into ziglang:master Apr 15, 2025
17 of 18 checks passed
@alexrp alexrp added this to the 0.14.1 milestone Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

error.Unexpected in std.os.windows.CreateProcess when cwd does doesnt exist or is invalid
3 participants