From cc87dfbb5256cd43d5c32efff8e3cd307e70168f Mon Sep 17 00:00:00 2001 From: Spekular Date: Wed, 30 Sep 2020 11:26:58 +0200 Subject: [PATCH] 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 --- src/core/PathUtil.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/PathUtil.cpp b/src/core/PathUtil.cpp index ab81c4941..5881db9f4 100644 --- a/src/core/PathUtil.cpp +++ b/src/core/PathUtil.cpp @@ -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; }