[llvm] r284384 - [Support] remove_dots: Remove .. from absolute paths.

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 17 06:28:21 PDT 2016


Author: d0k
Date: Mon Oct 17 08:28:21 2016
New Revision: 284384

URL: http://llvm.org/viewvc/llvm-project?rev=284384&view=rev
Log:
[Support] remove_dots: Remove .. from absolute paths.

/../foo is still a proper path after removing the dotdot. This should
now finally match https://9p.io/sys/doc/lexnames.html [Cleaning names].

Modified:
    llvm/trunk/lib/Support/Path.cpp
    llvm/trunk/unittests/Support/Path.cpp

Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=284384&r1=284383&r2=284384&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Mon Oct 17 08:28:21 2016
@@ -707,11 +707,14 @@ static SmallString<256> remove_dots(Stri
   for (StringRef C : llvm::make_range(path::begin(rel), path::end(rel))) {
     if (C == ".")
       continue;
-    // Leading ".." will remain in the path.
-    if (remove_dot_dot && C == ".." && !components.empty() &&
-        components.back() != "..") {
-      components.pop_back();
-      continue;
+    // Leading ".." will remain in the path unless it's at the root.
+    if (remove_dot_dot && C == "..") {
+      if (!components.empty() && components.back() != "..") {
+        components.pop_back();
+        continue;
+      }
+      if (path::is_absolute(path))
+        continue;
     }
     components.push_back(C);
   }

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=284384&r1=284383&r2=284384&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Mon Oct 17 08:28:21 2016
@@ -969,6 +969,8 @@ TEST(Support, RemoveDots) {
   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));
+  EXPECT_EQ("\\a\\c", remove_dots("\\..\\..\\a\\c", true));
+  EXPECT_EQ("\\a\\c", remove_dots("\\..\\a\\b\\\\..\\.\\.\\\\c", true));
 
   SmallString<64> Path1(".\\.\\c");
   EXPECT_TRUE(path::remove_dots(Path1, true));
@@ -982,6 +984,8 @@ TEST(Support, RemoveDots) {
   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));
+  EXPECT_EQ("/a/c", remove_dots("/../../a/c", true));
+  EXPECT_EQ("/a/c", remove_dots("/../a/b//../././/c", true));
 
   SmallString<64> Path1("././c");
   EXPECT_TRUE(path::remove_dots(Path1, true));




More information about the llvm-commits mailing list