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 {...@@ -1308,9 +1308,9 @@ pub const Dir = struct {
1308 if (end_index == sub_path.len) return;1308 if (end_index == sub_path.len) return;
1309 },1309 },
1310 error.FileNotFound => {1310 error.FileNotFound => {
1311 if (end_index == 0) return err;
1312 // march end_index backward until next path component1311 // march end_index backward until next path component
1313 while (true) {1312 while (true) {
1313 if (end_index == 0) return err;
1314 end_index -= 1;1314 end_index -= 1;
1315 if (path.isSep(sub_path[end_index])) break;1315 if (path.isSep(sub_path[end_index])) break;
1316 }1316 }
lib/std/fs/test.zig+10
...@@ -610,6 +610,16 @@ test "makePath, put some files in it, deleteTree" {...@@ -610,6 +610,16 @@ test "makePath, put some files in it, deleteTree" {
610 }610 }
611}611}
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
613test "writev, readv" {623test "writev, readv" {
614 var tmp = tmpDir(.{});624 var tmp = tmpDir(.{});
615 defer tmp.cleanup();625 defer tmp.cleanup();