From ad08117e9daf5d2a34be1ca71a23366f39079990 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 20 Dec 2025 20:15:14 -0800 Subject: [PATCH] Fix sizing of buffer/reservation size for dirReadWindows --- lib/std/Io/Dir.zig | 7 ++++++- lib/std/Io/Threaded.zig | 8 +++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/std/Io/Dir.zig b/lib/std/Io/Dir.zig index 60cdd88453d3f05241207412df41cf6b19688ef0..a84a20167dd82c29dde487f75d738ca992743b25 100644 --- a/lib/std/Io/Dir.zig +++ b/lib/std/Io/Dir.zig @@ -105,7 +105,12 @@ pub const Reader = struct { pub const min_buffer_len = switch (native_os) { .linux => std.mem.alignForward(usize, @sizeOf(std.os.linux.dirent64), 8) + std.mem.alignForward(usize, max_name_bytes, 8), - .windows => std.mem.alignForward(usize, max_name_bytes, @alignOf(usize)), + .windows => len: { + const max_info_len = @sizeOf(std.os.windows.FILE_BOTH_DIR_INFORMATION) + std.os.windows.NAME_MAX * 2; + const info_align = @alignOf(std.os.windows.FILE_BOTH_DIR_INFORMATION); + const reserved_len = std.mem.alignForward(usize, max_name_bytes, info_align) - max_info_len; + break :len std.mem.alignForward(usize, reserved_len, info_align) + max_info_len; + }, .wasi => @sizeOf(std.os.wasi.dirent_t) + std.mem.alignForward(usize, max_name_bytes, @alignOf(std.os.wasi.dirent_t)), else => if (builtin.link_libc) @sizeOf(std.c.dirent) else std.mem.alignForward(usize, max_name_bytes, @alignOf(usize)), diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index 182a8f4b6d45ae6561bb8ae6fca88a86d6bbe790..a586ae2b38acafee8df177d8055c382de5cf0010 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -3757,11 +3757,13 @@ fn dirReadWindows(userdata: ?*anyopaque, dr: *Dir.Reader, buffer: []Dir.Entry) D // reserve enough to get us to up to having `3 * NAME_MAX` bytes available when taking into account // that we have the ability to write over top of the reserved memory + the full footprint of that // particular `FILE_BOTH_DIR_INFORMATION`. - const reserve_needed = w.NAME_MAX - @sizeOf(w.FILE_BOTH_DIR_INFORMATION); - const unreserved_start = std.mem.alignForward(usize, reserve_needed, @alignOf(usize)); + const max_info_len = @sizeOf(w.FILE_BOTH_DIR_INFORMATION) + w.NAME_MAX * 2; + const info_align = @alignOf(w.FILE_BOTH_DIR_INFORMATION); + const reserve_needed = std.mem.alignForward(usize, Dir.max_name_bytes, info_align) - max_info_len; + const unreserved_start = std.mem.alignForward(usize, reserve_needed, info_align); const unreserved_buffer = dr.buffer[unreserved_start..]; // This is enforced by `Dir.Reader` - assert(unreserved_buffer.len >= @sizeOf(w.FILE_BOTH_DIR_INFORMATION) + w.NAME_MAX * 2); + assert(unreserved_buffer.len >= max_info_len); var name_index: usize = 0; var buffer_index: usize = 0; -- 2.54.0