From 442eb028c171bced12183f0258ad66929f8a5615 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Sun, 18 Nov 2018 20:25:19 +0100 Subject: [PATCH] wrealpath: Simplify - Reuse the buffer - Don't duplicate the code for no "/" --- src/wutil.cpp | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/wutil.cpp b/src/wutil.cpp index 9572ef3c3..8287b5bb6 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -400,6 +400,7 @@ maybe_t wrealpath(const wcstring &pathname) { if (narrow_res) { real_path.append(narrow_res); } else { + // Check if everything up to the last path component is valid. size_t pathsep_idx = narrow_path.rfind('/'); if (pathsep_idx == 0) { @@ -407,29 +408,23 @@ maybe_t wrealpath(const wcstring &pathname) { // single path component and thus doesn't need conversion. real_path = narrow_path; } else { - char tmpbuff[PATH_MAX]; - + // Only call realpath() on the portion up to the last component. + errno = 0; if (pathsep_idx == cstring::npos) { - // No pathsep means a single path component relative to pwd. - errno = 0; - narrow_res = realpath(".", tmpbuff); - - if (narrow_res == NULL) // likely no such file or dir - return none(); - - pathsep_idx = 0; + // If there is no "/", this is a file in $PWD, so give the realpath to that. + narrow_res = realpath(".", tmpbuf); } else { - errno = 0; - // Only call realpath() on the portion up to the last component. - // Be sure to include the last "/", so that the penultimate component is considered as a directory. + // Be sure to include the last "/" to have the penultimate component considered a directory. // Otherwise "file/something" succeeds. - narrow_res = realpath(narrow_path.substr(0, pathsep_idx + 1).c_str(), tmpbuff); - - if (!narrow_res) return none(); - - pathsep_idx++; + narrow_res = realpath(narrow_path.substr(0, pathsep_idx + 1).c_str(), tmpbuf); } - real_path.append(narrow_res); // if things went wrong, we are just appending NULL. + + if (!narrow_res) return none(); + + pathsep_idx++; + + real_path.append(narrow_res); + // This test is to deal with pathological cases such as /../../x => //x. if (real_path.size() > 1) real_path.append("/"); real_path.append(narrow_path.substr(pathsep_idx, cstring::npos));