diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-12-19 11:26:06 -0800 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-12-23 22:15:11 -0800 |
| commit | 002d444964869a5fd4d588d45c46f3acfa16cf5d (patch) | |
| tree | a92f8dcd1de9c85613a721d4f92f71f20bb520a4 /lib/std/Io/Threaded.zig | |
| parent | 018e34271fe58e540dc46e8bca529ce399625bef (diff) | |
| download | zig-002d444964869a5fd4d588d45c46f3acfa16cf5d.tar.gz zig-002d444964869a5fd4d588d45c46f3acfa16cf5d.zip | |
std: fix Io.Dir.min_buffer_len on Linux
Diffstat (limited to 'lib/std/Io/Threaded.zig')
| -rw-r--r-- | lib/std/Io/Threaded.zig | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index 81c18a8c74..3472b8fbe2 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -3399,7 +3399,10 @@ fn dirReadLinux(userdata: ?*anyopaque, dr: *Dir.Reader, buffer: []Dir.Entry) Dir dr.state = .finished; return 0; }, - .INVAL => return error.Unexpected, // Linux may in some cases return EINVAL when reading /proc/$PID/net. + // This can occur when reading /proc/$PID/net, or + // if the provided buffer is too small. Neither + // scenario is intended to be handled by this API. + .INVAL => return error.Unexpected, .ACCES => return error.AccessDenied, // Lacking permission to iterate this directory. else => |err| return posix.unexpectedErrno(err), } @@ -3413,11 +3416,19 @@ fn dirReadLinux(userdata: ?*anyopaque, dr: *Dir.Reader, buffer: []Dir.Entry) Dir dr.index = 0; dr.end = n; } - const linux_entry: *align(1) linux.dirent64 = @ptrCast(&dr.buffer[dr.index]); + // Linux aligns the header by padding after the null byte of the name + // to align the next entry. This means we can find the end of the name + // by looking at only the 8 bytes before the next record. However since + // file names are usually short it's better to keep the machine code + // simpler. + const linux_entry: *linux.dirent64 = @ptrCast(@alignCast(&dr.buffer[dr.index])); const next_index = dr.index + linux_entry.reclen; dr.index = next_index; + const name_ptr: [*]u8 = &linux_entry.name; + const padded_name = name_ptr[0 .. linux_entry.reclen - @offsetOf(linux.dirent64, "name")]; + const name_len = std.mem.findScalar(u8, padded_name, 0).?; + const name = name_ptr[0..name_len :0]; - const name = std.mem.sliceTo(@as([*:0]u8, @ptrCast(&linux_entry.name)), 0); if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) continue; const entry_kind: File.Kind = switch (linux_entry.type) { |
