Skip to content

Commit

Permalink
Stop passing NULL for realpath()'s second param
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ridiculousfish committed Oct 11, 2017
1 parent 639faf1 commit 05c0cb7
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/wutil.cpp
Expand Up @@ -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 {
Expand All @@ -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++;
}
Expand All @@ -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);
Expand Down

0 comments on commit 05c0cb7

Please sign in to comment.