mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
std.fs: prevent possible integer overflow in Dir.makePath
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
This commit is contained in:
parent
d66c61a2cf
commit
618398b7d3
@ -1308,9 +1308,9 @@ pub const Dir = struct {
|
||||
if (end_index == sub_path.len) return;
|
||||
},
|
||||
error.FileNotFound => {
|
||||
if (end_index == 0) return err;
|
||||
// march end_index backward until next path component
|
||||
while (true) {
|
||||
if (end_index == 0) return err;
|
||||
end_index -= 1;
|
||||
if (path.isSep(sub_path[end_index])) break;
|
||||
}
|
||||
|
||||
@ -610,6 +610,16 @@ test "makePath, put some files in it, deleteTree" {
|
||||
}
|
||||
}
|
||||
|
||||
test "makePath in a directory that no longer exists" {
|
||||
if (builtin.os.tag == .windows) return error.SkipZigTest; // Windows returns FileBusy if attempting to remove an open dir
|
||||
|
||||
var tmp = tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
try tmp.parent_dir.deleteTree(&tmp.sub_path);
|
||||
|
||||
try testing.expectError(error.FileNotFound, tmp.dir.makePath("sub-path"));
|
||||
}
|
||||
|
||||
test "writev, readv" {
|
||||
var tmp = tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user