Fix relativeOrAbsolute bug with baseQDir in PathUtils.cpp (#5689)

* Fix relativeOrAbsolute bug with baseQDir in PathUtils.cpp

If `base` is `Absolute`, `baseQDir(base)` will point to the working directory. It will result in undefined behaviors.

To fix this, update relativeOrAbsolute to explicitly return an absolute path when the target base is Absolute. Also make baseQDir return QDir::root() for Absolute base instead of QDir(""), because the latter represents the working directory.

Co-authored-by: Hyunjin Song <tteu.ingog@gmail.com>
This commit is contained in:
Spekular
2020-09-30 11:26:58 +02:00
committed by GitHub
parent 31f79a2263
commit cc87dfbb52

View File

@@ -36,7 +36,11 @@ namespace PathUtil
return QDir::cleanPath(loc) + "/";
}
QDir baseQDir (const Base base) { return QDir(baseLocation(base)); }
QDir baseQDir (const Base base)
{
if (base == Base::Absolute) { return QDir::root(); }
return QDir(baseLocation(base));
}
QString basePrefix(const Base base)
{
@@ -123,6 +127,7 @@ namespace PathUtil
{
if (input.isEmpty()) { return input; }
QString absolutePath = toAbsolute(input);
if (base == Base::Absolute) { return absolutePath; }
QString relativePath = baseQDir(base).relativeFilePath(absolutePath);
return relativePath.startsWith("..") ? absolutePath : relativePath;
}