Fix tilde expansion algorithm

In principle correct, in practice completely broken.

I wrongly assumed basic_string::find() would behave like strcspn(),
and also used a nonsensical string index to check for the tilde.
This commit is contained in:
Přemysl Eric Janouch 2020-09-28 01:58:55 +02:00
parent b99f96cf6a
commit 98c8dc5b7d
Signed by: p
GPG Key ID: A0420B94F92B9493

View File

@ -120,11 +120,11 @@ fun untilde (const string &path) -> string {
string tail = path.substr (1); string tail = path.substr (1);
if (path[0] == '\\') if (path[0] == '\\')
return tail; return tail;
if (path[1] != '~') if (path[0] != '~')
return path; return path;
// If there is something between the ~ and the first / (or the EOS) // If there is something between the ~ and the first / (or the EOS)
if (size_t until_slash = tail.find ('/')) { if (size_t until_slash = strcspn (tail.c_str (), "/")) {
if (const auto *pw = getpwnam (tail.substr (0, until_slash).c_str ())) if (const auto *pw = getpwnam (tail.substr (0, until_slash).c_str ()))
return pw->pw_dir + tail.substr (until_slash); return pw->pw_dir + tail.substr (until_slash);
} else if (const auto *home = getenv ("HOME")) { } else if (const auto *home = getenv ("HOME")) {