From 3b46b7c0af9cbd545c017e21cd245cff4790363b Mon Sep 17 00:00:00 2001 From: Ben Boeckel <ben.boeckel@kitware.com> Date: Wed, 24 Jan 2018 14:10:16 -0500 Subject: [PATCH] ConvertToUnixSlashes: bail early on empty paths --- SystemTools.cxx | 53 ++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/SystemTools.cxx b/SystemTools.cxx index 71d05ffc..53cd02cc 100644 --- a/SystemTools.cxx +++ b/SystemTools.cxx @@ -1884,6 +1884,10 @@ static void ConvertVMSToUnix(std::string& path) // convert windows slashes to unix slashes void SystemTools::ConvertToUnixSlashes(std::string& path) { + if (path.empty()) { + return; + } + const char* pathCString = path.c_str(); bool hasDoubleSlash = false; #ifdef __VMS @@ -1915,36 +1919,35 @@ void SystemTools::ConvertToUnixSlashes(std::string& path) SystemTools::ReplaceString(path, "//", "/"); } #endif + // remove any trailing slash - if (!path.empty()) { - // if there is a tilda ~ then replace it with HOME - pathCString = path.c_str(); - if (pathCString[0] == '~' && - (pathCString[1] == '/' || pathCString[1] == '\0')) { - std::string homeEnv; - if (SystemTools::GetEnv("HOME", homeEnv)) { - path.replace(0, 1, homeEnv); - } + // if there is a tilda ~ then replace it with HOME + pathCString = path.c_str(); + if (pathCString[0] == '~' && + (pathCString[1] == '/' || pathCString[1] == '\0')) { + std::string homeEnv; + if (SystemTools::GetEnv("HOME", homeEnv)) { + path.replace(0, 1, homeEnv); } + } #ifdef HAVE_GETPWNAM - else if (pathCString[0] == '~') { - std::string::size_type idx = path.find_first_of("/\0"); - std::string user = path.substr(1, idx - 1); - passwd* pw = getpwnam(user.c_str()); - if (pw) { - path.replace(0, idx, pw->pw_dir); - } + else if (pathCString[0] == '~') { + std::string::size_type idx = path.find_first_of("/\0"); + std::string user = path.substr(1, idx - 1); + passwd* pw = getpwnam(user.c_str()); + if (pw) { + path.replace(0, idx, pw->pw_dir); } + } #endif - // remove trailing slash if the path is more than - // a single / - pathCString = path.c_str(); - size_t size = path.size(); - if (size > 1 && *path.rbegin() == '/') { - // if it is c:/ then do not remove the trailing slash - if (!((size == 3 && pathCString[1] == ':'))) { - path.resize(size - 1); - } + // remove trailing slash if the path is more than + // a single / + pathCString = path.c_str(); + size_t size = path.size(); + if (size > 1 && *path.rbegin() == '/') { + // if it is c:/ then do not remove the trailing slash + if (!((size == 3 && pathCString[1] == ':'))) { + path.resize(size - 1); } } } -- GitLab