From c7057bd25b2a99e5ac61963efd588f5fe4ba93c6 Mon Sep 17 00:00:00 2001 From: Bodie Solomon Date: Mon, 18 Jun 2018 07:37:26 -0400 Subject: [PATCH] Fix 1117: Revise realpath scratch logic --- src/os.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/os.cpp b/src/os.cpp index 0e3c3f9a60..75f748e2f0 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -994,23 +994,24 @@ int os_self_exe_path(Buf *out_path) { int ret1 = _NSGetExecutablePath(nullptr, &u32_len); assert(ret1 != 0); - // 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); + // Make a buffer having room for the temp path. + Buf *tmp = buf_alloc_fixed(u32_len); // Fill the executable path. int ret2 = _NSGetExecutablePath(buf_ptr(tmp), &u32_len); assert(ret2 == 0); // Resolve the real path from that. - char *real_path = realpath(buf_ptr(tmp), buf_ptr(resolved)); - assert(real_path == buf_ptr(resolved)); + buf_resize(out_path, PATH_MAX); + char *real_path = realpath(buf_ptr(tmp), buf_ptr(out_path)); + assert(real_path == buf_ptr(out_path)); + + // Deallocate our scratch space. + buf_deinit(tmp); + + // Resize out_path for the correct length. + buf_resize(out_path, strlen(buf_ptr(out_path))); - // 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);