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 14:31:28+02:00
log69007f096177143086e28da0dc1a0eff4efcc52c
treef5ff3ea30042bc0790f478085fda4413a1f650d2
parentc06fecd46657b173763087495fe20fd720b2d210

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
...@@ -1349,13 +1349,11 @@ pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathErr...@@ -1349,13 +1349,11 @@ pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathErr
13491349
1350 var wide_buf: [w.PATH_MAX_WIDE]u16 = undefined;1350 var wide_buf: [w.PATH_MAX_WIDE]u16 = undefined;
1351 const wide_slice = try w.GetFinalPathNameByHandle(h_file, .{}, &wide_buf);1351 const wide_slice = try w.GetFinalPathNameByHandle(h_file, .{}, &wide_buf);
1352 var big_out_buf: [fs.max_path_bytes]u8 = undefined;1352 const len = std.unicode.calcWtf8Len(wide_slice);
1353 const end_index = std.unicode.wtf16LeToWtf8(&big_out_buf, wide_slice);1353 if (len > out_buffer.len)
1354 if (end_index > out_buffer.len)
1355 return error.NameTooLong;1354 return error.NameTooLong;
1356 const result = out_buffer[0..end_index];1355 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
1357 @memcpy(result, big_out_buf[0..end_index]);1356 return out_buffer[0..end_index];
1358 return result;
1359}1357}
13601358
1361pub const RealPathAllocError = RealPathError || Allocator.Error;1359pub const RealPathAllocError = RealPathError || Allocator.Error;