Avoid mutating mybasename and mydirname parameters (#2004)

basename and dirname mutate their inputs but const_cast hid this
behavior.  Also shuffle helpers to avoid unnecessary std::string.
Follows on to 404c284440.
This commit is contained in:
Andrew Gaul 2022-07-28 23:34:01 +09:00 committed by GitHub
parent e30a5939d0
commit f9f614a474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -225,7 +225,7 @@ std::string mydirname(const std::string& path)
{
AutoLock auto_lock(pbasename_lock);
return std::string(dirname(const_cast<char*>(path.c_str())));
return mydirname(path.c_str());
}
// safe variant of dirname
@ -235,14 +235,18 @@ std::string mydirname(const char* path)
if(!path || '\0' == path[0]){
return std::string("");
}
return mydirname(std::string(path));
char *buf = strdup(path);
std::string result = dirname(buf);
free(buf);
return result;
}
std::string mybasename(const std::string& path)
{
AutoLock auto_lock(pbasename_lock);
return std::string(basename(const_cast<char*>(path.c_str())));
return mybasename(path.c_str());
}
// safe variant of basename
@ -252,7 +256,11 @@ std::string mybasename(const char* path)
if(!path || '\0' == path[0]){
return std::string("");
}
return mybasename(std::string(path));
char *buf = strdup(path);
std::string result = basename(buf);
free(buf);
return result;
}
// mkdir --parents