authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2022-04-06 23:48:30-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-04-15 11:19:23+03:00
log618398b7d3c0df13dcb3d87540e400665b2c02dc
tree3bd67392b54ffcbc38ed178bbff0119abdce1811
parentd66c61a2cf69665223815a1a12a1f93b30b99571

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

2 files changed, 11 insertions(+), 1 deletions(-)

lib/std/fs.zig+1-1
......@@ -1308,9 +1308,9 @@ pub const Dir = struct {
13081308 if (end_index == sub_path.len) return;
13091309 },
13101310 error.FileNotFound => {
1311 if (end_index == 0) return err;
13121311 // march end_index backward until next path component
13131312 while (true) {
1313 if (end_index == 0) return err;
13141314 end_index -= 1;
13151315 if (path.isSep(sub_path[end_index])) break;
13161316 }
lib/std/fs/test.zig+10
......@@ -610,6 +610,16 @@ test "makePath, put some files in it, deleteTree" {
610610 }
611611}
612612
613test "makePath in a directory that no longer exists" {
614 if (builtin.os.tag == .windows) return error.SkipZigTest; // Windows returns FileBusy if attempting to remove an open dir
615
616 var tmp = tmpDir(.{});
617 defer tmp.cleanup();
618 try tmp.parent_dir.deleteTree(&tmp.sub_path);
619
620 try testing.expectError(error.FileNotFound, tmp.dir.makePath("sub-path"));
621}
622
613623test "writev, readv" {
614624 var tmp = tmpDir(.{});
615625 defer tmp.cleanup();