Skip to content
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

std.fs.cwd.makePath: integer overflow if CWD does not exist #11397

Closed
ehaas opened this issue Apr 6, 2022 · 3 comments · Fixed by #11400
Closed

std.fs.cwd.makePath: integer overflow if CWD does not exist #11397

ehaas opened this issue Apr 6, 2022 · 3 comments · Fixed by #11400
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@ehaas
Copy link
Sponsor Contributor

ehaas commented Apr 6, 2022

Zig Version

0.10.0-dev.1575+7f91be9c8

Steps to Reproduce

Run zig test on the following:

const std = @import("std");

test "cwd.makePath with non-existent CWD" {
    var tmp_dir = std.testing.tmpDir(.{});
    try tmp_dir.dir.setAsCwd();
    tmp_dir.cleanup();

    const cwd = std.fs.cwd();
    try cwd.makePath("sub-path");
}

Expected Behavior

The equivalent C code will set errno to ENOENT if trying to create a directory within a non-existent directory, so I would expect the test to fail with error.NotFound.

Actual Behavior

Test [1/1] test "cwd.makePath with non-existent CWD"... thread 42590880 panic: integer overflow
/Users/ehaas/source/zig/build/lib/zig/std/fs.zig:1314:35: 0x108c24ce8 in std.fs.Dir.makePath (test)
                        end_index -= 1;
                                  ^
/Users/ehaas/test.zig:9:21: 0x108c235cc in test "cwd.makePath with non-existent CWD" (test)
    try cwd.makePath("sub-path");
                    ^
@ehaas ehaas added the bug Observed behavior contradicts documented or intended behavior label Apr 6, 2022
@ominitay
Copy link
Contributor

ominitay commented Apr 6, 2022

I don't think so?

@ominitay
Copy link
Contributor

ominitay commented Apr 6, 2022

Wouldn't changing if (path.isSep(sub_path[end_index])) break; to if (path.isSep(sub_path[end_index]) or end_index == 0) break; fix this?

@ehaas
Copy link
Sponsor Contributor Author

ehaas commented Apr 6, 2022

Wouldn't changing if (path.isSep(sub_path[end_index])) break; to if (path.isSep(sub_path[end_index]) or end_index == 0) break; fix this?

That works, but it does result in an extra call to makeDir with an empty string. I think we probably want to check for end_index == 0 inside the while loop.

ehaas added a commit to ehaas/zig that referenced this issue Apr 7, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
ehaas added a commit to ehaas/zig that referenced this issue Apr 7, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
ehaas added a commit to ehaas/zig that referenced this issue Apr 7, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
ehaas added a commit to ehaas/zig that referenced this issue Apr 7, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
ehaas added a commit to ehaas/zig that referenced this issue Apr 8, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
ehaas added a commit to ehaas/zig that referenced this issue Apr 9, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
ehaas added a commit to ehaas/zig that referenced this issue Apr 9, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
ehaas added a commit to ehaas/zig that referenced this issue Apr 15, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
ehaas added a commit to ehaas/zig that referenced this issue Apr 15, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes ziglang#11397
Vexu pushed a commit that referenced this issue Apr 15, 2022
The call to `makeDir` for the top-level component of `sub_path`
can return `error.FileNotFound` if the directory represented by
`self` has been deleted.

Fixes #11397
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants