summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/Support/Path.h3
-rw-r--r--llvm/lib/Support/Path.cpp11
-rw-r--r--llvm/unittests/Support/Path.cpp4
3 files changed, 11 insertions, 7 deletions
diff --git a/llvm/include/llvm/Support/Path.h b/llvm/include/llvm/Support/Path.h
index 853f0997571..0513350d446 100644
--- a/llvm/include/llvm/Support/Path.h
+++ b/llvm/include/llvm/Support/Path.h
@@ -445,7 +445,8 @@ StringRef remove_leading_dotslash(StringRef path);
/// @brief In-place remove any './' and optionally '../' components from a path.
///
/// @param path processed path
-/// @param remove_dot_dot specify if '../' should be removed
+/// @param remove_dot_dot specify if '../' (except for leading "../") should be
+/// removed
/// @result True if path was changed
bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false);
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index f6355d1df80..4374cba4988 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -707,12 +707,11 @@ static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot) {
for (StringRef C : llvm::make_range(path::begin(rel), path::end(rel))) {
if (C == ".")
continue;
- if (remove_dot_dot) {
- if (C == "..") {
- if (!components.empty())
- components.pop_back();
- continue;
- }
+ // Leading ".." will remain in the path.
+ if (remove_dot_dot && C == ".." && !components.empty() &&
+ components.back() != "..") {
+ components.pop_back();
+ continue;
}
components.push_back(C);
}
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index bd12b3b8c84..e3b543c7bfe 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -965,6 +965,8 @@ TEST(Support, RemoveDots) {
EXPECT_EQ("a\\..\\b\\c", remove_dots(".\\a\\..\\b\\c", false));
EXPECT_EQ("b\\c", remove_dots(".\\a\\..\\b\\c", true));
EXPECT_EQ("c", remove_dots(".\\.\\c", true));
+ EXPECT_EQ("..\\a\\c", remove_dots("..\\a\\b\\..\\c", true));
+ EXPECT_EQ("..\\..\\a\\c", remove_dots("..\\..\\a\\b\\..\\c", true));
SmallString<64> Path1(".\\.\\c");
EXPECT_TRUE(path::remove_dots(Path1, true));
@@ -976,6 +978,8 @@ TEST(Support, RemoveDots) {
EXPECT_EQ("a/../b/c", remove_dots("./a/../b/c", false));
EXPECT_EQ("b/c", remove_dots("./a/../b/c", true));
EXPECT_EQ("c", remove_dots("././c", true));
+ EXPECT_EQ("../a/c", remove_dots("../a/b/../c", true));
+ EXPECT_EQ("../../a/c", remove_dots("../../a/b/../c", true));
SmallString<64> Path1("././c");
EXPECT_TRUE(path::remove_dots(Path1, true));
OpenPOWER on IntegriCloud