From c8425f832cadb25e6c689f367d59adda7e22333f Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 26 Sep 2017 09:49:54 -0500 Subject: [PATCH] Fix non-standard getcwd() invocation The POSIX standard specifies that a buffer should be supplied to getcwd(), not doing so is undefined (or rather, platform-defined) behavior. This was causing the getcwd errors on illumos (though not seen on Solaris 11) reported in #3340 Closes #3340 (cherry picked from commit b495c68f28b44e0d9065c7cd2d73712b8283a4c3) --- src/wutil.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/wutil.cpp b/src/wutil.cpp index ab5de1d76..0ab3dca6a 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -137,18 +137,14 @@ bool wreaddir_for_dirs(DIR *dir, wcstring *out_name) { } const wcstring wgetcwd() { - wcstring retval; - - char *res = getcwd(NULL, 0); + char cwd[NAME_MAX]; + char *res = getcwd(cwd, sizeof(cwd)); if (res) { - retval = str2wcstring(res); - free(res); - } else { - debug(0, _(L"getcwd() failed with errno %d/%s"), errno, strerror(errno)); - retval = wcstring(); + return str2wcstring(res); } - return retval; + debug(0, _(L"getcwd() failed with errno %d/%s"), errno, strerror(errno)); + return wcstring(); } int wchdir(const wcstring &dir) {