Stop passing NULL for realpath()'s second param

macOS 10.5 and earlier do not support the convention of returning
a dynamically allocated string, plus this seems like an unnecessary
malloc. Always allocate a buffer for realpath() to write into.
This commit is contained in:
ridiculousfish 2017-10-10 23:31:27 -07:00
parent 639faf1c7f
commit 05c0cb713d

View file

@ -350,7 +350,8 @@ wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path) {
narrow_path.erase(narrow_path.size() - 1, 1);
}
char *narrow_res = realpath(narrow_path.c_str(), NULL);
char tmpbuf[PATH_MAX];
char *narrow_res = realpath(narrow_path.c_str(), tmpbuf);
if (narrow_res) {
real_path.append(narrow_res);
} else {
@ -360,14 +361,15 @@ wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path) {
// single path component and thus doesn't need conversion.
real_path = narrow_path;
} else {
char tmpbuff[PATH_MAX];
if (pathsep_idx == cstring::npos) {
// No pathsep means a single path component relative to pwd.
narrow_res = realpath(".", NULL);
assert(narrow_res != NULL);
narrow_res = realpath(".", tmpbuff);
assert(narrow_res != NULL && "realpath unexpectedly returned null");
pathsep_idx = 0;
} else {
// Only call realpath() on the portion up to the last component.
narrow_res = realpath(narrow_path.substr(0, pathsep_idx).c_str(), NULL);
narrow_res = realpath(narrow_path.substr(0, pathsep_idx).c_str(), tmpbuff);
if (!narrow_res) return NULL;
pathsep_idx++;
}
@ -377,14 +379,6 @@ wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path) {
real_path.append(narrow_path.substr(pathsep_idx, cstring::npos));
}
}
#if __APPLE__ && __DARWIN_C_LEVEL < 200809L
// OS X Snow Leopard is broken with respect to the dynamically allocated buffer returned by
// realpath(). It's not dynamically allocated so attempting to free that buffer triggers a
// malloc/free error. Thus we don't attempt the free in this case.
#else
free(narrow_res);
#endif
wcstring wreal_path = str2wcstring(real_path);
if (resolved_path) {
wcslcpy(resolved_path, wreal_path.c_str(), PATH_MAX);