Commit a7dfc6470f

Michael Pfaff <michael@pfaff.dev>
2025-04-23 16:38:35
Reuse pathname_w buffer as out_buffer when calling realpathW
1 parent 2886a8b
Changed files (3)
lib/std/fs/Dir.zig
@@ -1369,10 +1369,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
         @compileError("realpath is not available on WASI");
     }
     if (native_os == .windows) {
-        const pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
+        var pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
 
-        var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
-        const wide_slice = try self.realpathW(pathname_w.span(), &wide_buf);
+        const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
 
         const len = std.unicode.calcWtf8Len(wide_slice);
         if (len > out_buffer.len)
@@ -1389,10 +1388,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
 /// See also `Dir.realpath`, `realpathZ`.
 pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathError![]u8 {
     if (native_os == .windows) {
-        const pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
+        var pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
 
-        var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
-        const wide_slice = try self.realpathW(pathname_w.span(), &wide_buf);
+        const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
 
         const len = std.unicode.calcWtf8Len(wide_slice);
         if (len > out_buffer.len)
lib/std/fs.zig
@@ -642,10 +642,9 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
             // If ImagePathName is a symlink, then it will contain the path of the
             // symlink, not the path that the symlink points to. We want the path
             // that the symlink points to, though, so we need to get the realpath.
-            const pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
+            var pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
 
-            var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
-            const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &wide_buf) catch |err| switch (err) {
+            const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
                 error.InvalidWtf8 => unreachable,
                 else => |e| return e,
             };
lib/std/posix.zig
@@ -5675,10 +5675,9 @@ pub const RealPathError = error{
 /// Calling this function is usually a bug.
 pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
     if (native_os == .windows) {
-        const pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
+        var pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
 
-        var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
-        const wide_slice = try realpathW(pathname_w.span(), &wide_buf);
+        const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
 
         const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
         return out_buffer[0..end_index];
@@ -5694,10 +5693,9 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE
 /// Calling this function is usually a bug.
 pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
     if (native_os == .windows) {
-        const pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
+        var pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
 
-        var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
-        const wide_slice = try realpathW(pathname_w.span(), &wide_buf);
+        const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
 
         const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
         return out_buffer[0..end_index];