Merge pull request #7160 from semarie/openbsd-getdents

openbsd: getdents could return invalid entry with d_fileno==0
master
Jakub Konka 2020-11-20 00:23:57 +01:00 committed by GitHub
commit c25cb39d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 6 deletions

View File

@ -352,7 +352,7 @@ pub const Dir = struct {
const name = @ptrCast([*]u8, &darwin_entry.d_name)[0..darwin_entry.d_namlen];
if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) {
if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..") or (darwin_entry.d_ino == 0)) {
continue :start_over;
}
@ -393,17 +393,24 @@ pub const Dir = struct {
self.index = 0;
self.end_index = @intCast(usize, rc);
}
const freebsd_entry = @ptrCast(*align(1) os.dirent, &self.buf[self.index]);
const next_index = self.index + freebsd_entry.reclen();
const bsd_entry = @ptrCast(*align(1) os.dirent, &self.buf[self.index]);
const next_index = self.index + bsd_entry.reclen();
self.index = next_index;
const name = @ptrCast([*]u8, &freebsd_entry.d_name)[0..freebsd_entry.d_namlen];
const name = @ptrCast([*]u8, &bsd_entry.d_name)[0..bsd_entry.d_namlen];
if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) {
const skip_zero_fileno = switch (builtin.os.tag) {
// d_fileno=0 is used to mark invalid entries or deleted files.
.openbsd, .netbsd => true,
else => false,
};
if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..") or
(skip_zero_fileno and bsd_entry.d_fileno == 0))
{
continue :start_over;
}
const entry_kind = switch (freebsd_entry.d_type) {
const entry_kind = switch (bsd_entry.d_type) {
os.DT_BLK => Entry.Kind.BlockDevice,
os.DT_CHR => Entry.Kind.CharacterDevice,
os.DT_DIR => Entry.Kind.Directory,