authorgravatar for bodie@synapsegarden.netBodie Solomon <bodie@synapsegarden.net> 2018-06-17 14:35:00-04:00
committergravatar for bodie@synapsegarden.netBodie Solomon <bodie@synapsegarden.net> 2018-06-18 07:40:31-04:00
loge6b69151c0d30d2df95d1f92b65784eac7d186d9
treee92630f1caed328d5e95347d5dcecb45949c0319
parentd52ef95f77478582e5c479cc2bd0a4ecb5591933
signature Commit is signed but in an unrecognized format.

Fix 1117: Use realpath in stage1 Darwin os_self_exe_path

Issue: https://github.com/ziglang/zig/issues/1117 The macOS stage1 Zig compiler should look in Zig's real absolute path for the Zig stdlib, but os_self_exe_path looks in its path as returned by _NSGetExecutablePath, which may be a symlink. This means that a symlinked Zig cannot find the Zig stdlib. This patch fixes the issue by resolving the _NSGetExecutablePath result to the real path using realpath() before copying the result to the output path.

1 files changed, 18 insertions(+), 2 deletions(-)

src/os.cpp+18-2
......@@ -989,12 +989,28 @@ int os_self_exe_path(Buf *out_path) {
989989 }
990990
991991#elif defined(ZIG_OS_DARWIN)
992 // How long is the executable's path?
992993 uint32_t u32_len = 0;
993994 int ret1 = _NSGetExecutablePath(nullptr, &u32_len);
994995 assert(ret1 != 0);
995 buf_resize(out_path, u32_len);
996 int ret2 = _NSGetExecutablePath(buf_ptr(out_path), &u32_len);
996
997 // Allocate a buffer for this path.
998 Buf *path_tmp = buf_alloc_fixed(u32_len);
999 // Fill the buffer with the path, which may be a symlink.
1000 int ret2 = _NSGetExecutablePath(buf_ptr(path_tmp), &u32_len);
9971001 assert(ret2 == 0);
1002
1003 // Make a buffer with room for the real path.
1004 Buf *resolve_tmp = buf_alloc_fixed(PATH_MAX);
1005
1006 // Fill it with the real resolved path.
1007 char *real_path = realpath(buf_ptr(path_tmp), buf_ptr(resolve_tmp));
1008 // IEEE Std 1003.1-2017: realpath() shall return a pointer to the
1009 // buffer containing the resolved name.
1010 assert(real_path == buf_ptr(resolve_tmp));
1011
1012 // Resize out_path and copy the resulting resolved absolute path.
1013 buf_init_from_buf(out_path, resolve_tmp);
9981014 return 0;
9991015#elif defined(ZIG_OS_LINUX)
10001016 buf_resize(out_path, 256);