Skip to content

Commit a7dfc64

Browse files
mpfaffsqueek502
authored andcommitted
Reuse pathname_w buffer as out_buffer when calling realpathW
1 parent 2886a8b commit a7dfc64

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

lib/std/fs.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,9 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
642642
// If ImagePathName is a symlink, then it will contain the path of the
643643
// symlink, not the path that the symlink points to. We want the path
644644
// that the symlink points to, though, so we need to get the realpath.
645-
const pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
645+
var pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
646646

647-
var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
648-
const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &wide_buf) catch |err| switch (err) {
647+
const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
649648
error.InvalidWtf8 => unreachable,
650649
else => |e| return e,
651650
};

lib/std/fs/Dir.zig

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,10 +1369,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
13691369
@compileError("realpath is not available on WASI");
13701370
}
13711371
if (native_os == .windows) {
1372-
const pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
1372+
var pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
13731373

1374-
var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
1375-
const wide_slice = try self.realpathW(pathname_w.span(), &wide_buf);
1374+
const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
13761375

13771376
const len = std.unicode.calcWtf8Len(wide_slice);
13781377
if (len > out_buffer.len)
@@ -1389,10 +1388,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
13891388
/// See also `Dir.realpath`, `realpathZ`.
13901389
pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathError![]u8 {
13911390
if (native_os == .windows) {
1392-
const pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
1391+
var pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
13931392

1394-
var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
1395-
const wide_slice = try self.realpathW(pathname_w.span(), &wide_buf);
1393+
const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
13961394

13971395
const len = std.unicode.calcWtf8Len(wide_slice);
13981396
if (len > out_buffer.len)

lib/std/posix.zig

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5675,10 +5675,9 @@ pub const RealPathError = error{
56755675
/// Calling this function is usually a bug.
56765676
pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
56775677
if (native_os == .windows) {
5678-
const pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
5678+
var pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
56795679

5680-
var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
5681-
const wide_slice = try realpathW(pathname_w.span(), &wide_buf);
5680+
const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
56825681

56835682
const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
56845683
return out_buffer[0..end_index];
@@ -5694,10 +5693,9 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE
56945693
/// Calling this function is usually a bug.
56955694
pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
56965695
if (native_os == .windows) {
5697-
const pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
5696+
var pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
56985697

5699-
var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
5700-
const wide_slice = try realpathW(pathname_w.span(), &wide_buf);
5698+
const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
57015699

57025700
const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
57035701
return out_buffer[0..end_index];

0 commit comments

Comments
 (0)