authorgravatar for michael@pfaff.devMichael Pfaff <michael@pfaff.dev> 2025-04-23 10:11:09-04:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-04-26 15:07:26+02:00
log53f298cffa5bbec4c4faa854afb56a5f2e23de48
treef48acdfea45ef83447b590d347275dcac88b85d6
parent3ca0f18bfe2e8a47c38b05fffea97146476e938c
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Calculate WTF-8 length before converting instead of converting into an intermediate buffer on the stack


1 files changed, 4 insertions(+), 6 deletions(-)

lib/std/fs/Dir.zig+4-6
...@@ -1347,13 +1347,11 @@ pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathErr...@@ -1347,13 +1347,11 @@ pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathErr
13471347
1348 var wide_buf: [w.PATH_MAX_WIDE]u16 = undefined;1348 var wide_buf: [w.PATH_MAX_WIDE]u16 = undefined;
1349 const wide_slice = try w.GetFinalPathNameByHandle(h_file, .{}, &wide_buf);1349 const wide_slice = try w.GetFinalPathNameByHandle(h_file, .{}, &wide_buf);
1350 var big_out_buf: [fs.max_path_bytes]u8 = undefined;1350 const len = std.unicode.calcWtf8Len(wide_slice);
1351 const end_index = std.unicode.wtf16LeToWtf8(&big_out_buf, wide_slice);1351 if (len > out_buffer.len)
1352 if (end_index > out_buffer.len)
1353 return error.NameTooLong;1352 return error.NameTooLong;
1354 const result = out_buffer[0..end_index];1353 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
1355 @memcpy(result, big_out_buf[0..end_index]);1354 return out_buffer[0..end_index];
1356 return result;
1357}1355}
13581356
1359pub const RealPathAllocError = RealPathError || Allocator.Error;1357pub const RealPathAllocError = RealPathError || Allocator.Error;