diff --git a/src/os.cpp b/src/os.cpp index 75e6dd8658..0e3c3f9a60 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -994,23 +994,23 @@ int os_self_exe_path(Buf *out_path) { int ret1 = _NSGetExecutablePath(nullptr, &u32_len); assert(ret1 != 0); - // Allocate a buffer for this path. - Buf *path_tmp = buf_alloc_fixed(u32_len); - // Fill the buffer with the path, which may be a symlink. - int ret2 = _NSGetExecutablePath(buf_ptr(path_tmp), &u32_len); + // Make room for the executable path and resolved path in out_path, + // then subslice it for convenience. + buf_resize(out_path, u32_len + PATH_MAX); + Buf *tmp = buf_slice(out_path, 0, u32_len); + Buf *resolved = buf_slice(out_path, u32_len, u32_len + PATH_MAX); + + // Fill the executable path. + int ret2 = _NSGetExecutablePath(buf_ptr(tmp), &u32_len); assert(ret2 == 0); - // Make a buffer with room for the real path. - Buf *resolve_tmp = buf_alloc_fixed(PATH_MAX); + // Resolve the real path from that. + char *real_path = realpath(buf_ptr(tmp), buf_ptr(resolved)); + assert(real_path == buf_ptr(resolved)); - // Fill it with the real resolved path. - char *real_path = realpath(buf_ptr(path_tmp), buf_ptr(resolve_tmp)); - // IEEE Std 1003.1-2017: realpath() shall return a pointer to the - // buffer containing the resolved name. - assert(real_path == buf_ptr(resolve_tmp)); - - // Resize out_path and copy the resulting resolved absolute path. - buf_init_from_buf(out_path, resolve_tmp); + // Write the real path back into the beginning of out_path, resize. + buf_init_from_buf(out_path, resolved); + assert(buf_len(out_path) == buf_len(resolved)); return 0; #elif defined(ZIG_OS_LINUX) buf_resize(out_path, 256);