Merge pull request #7578 from vrischmann/walker

fix segfault in std.fs.Walker
This commit is contained in:
Andrew Kelley 2021-01-01 13:28:01 -08:00 committed by GitHub
commit 99203e4177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -2183,7 +2183,7 @@ pub const Walker = struct {
while (true) {
if (self.stack.items.len == 0) return null;
// `top` becomes invalid after appending to `self.stack`.
const top = &self.stack.items[self.stack.items.len - 1];
var top = &self.stack.items[self.stack.items.len - 1];
const dirname_len = top.dirname_len;
if (try top.dir_it.next()) |base| {
self.name_buffer.shrink(dirname_len);
@ -2200,6 +2200,7 @@ pub const Walker = struct {
.dir_it = new_dir.iterate(),
.dirname_len = self.name_buffer.items.len,
});
top = &self.stack.items[self.stack.items.len - 1];
}
}
return Entry{

View File

@ -794,3 +794,42 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
try fs.deleteFileAbsolute(filename);
}
test "walker" {
if (builtin.os.tag == .wasi) return error.SkipZigTest;
var arena = ArenaAllocator.init(testing.allocator);
defer arena.deinit();
var allocator = &arena.allocator;
var tmp = tmpDir(.{});
defer tmp.cleanup();
const nb_dirs = 8;
var i: usize = 0;
var sub_dir = tmp.dir;
while (i < nb_dirs) : (i += 1) {
const dir_name = try std.fmt.allocPrint(allocator, "{}", .{i});
try sub_dir.makeDir(dir_name);
sub_dir = try sub_dir.openDir(dir_name, .{});
}
const tmp_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] });
var walker = try fs.walkPath(testing.allocator, tmp_path);
defer walker.deinit();
i = 0;
var expected_dir_name: []const u8 = "";
while (i < nb_dirs) : (i += 1) {
const name = try std.fmt.allocPrint(allocator, "{}", .{i});
expected_dir_name = if (expected_dir_name.len == 0)
name
else
try fs.path.join(allocator, &[_][]const u8{ expected_dir_name, name });
var entry = (try walker.next()).?;
testing.expectEqualStrings(expected_dir_name, try fs.path.relative(allocator, tmp_path, entry.path));
}
}