[llvm] r322095 - [Support] Use realpath(3) instead of trying to open a file.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 9 09:27:45 PST 2018


Author: davide
Date: Tue Jan  9 09:27:45 2018
New Revision: 322095

URL: http://llvm.org/viewvc/llvm-project?rev=322095&view=rev
Log:
[Support] Use realpath(3) instead of trying to open a file.

If we don't have read permissions on the directory the call would
fail.

<rdar://problem/35871293>

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

Modified: llvm/trunk/lib/Support/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Path.inc?rev=322095&r1=322094&r2=322095&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc (original)
+++ llvm/trunk/lib/Support/Unix/Path.inc Tue Jan  9 09:27:45 2018
@@ -860,12 +860,12 @@ std::error_code real_path(const Twine &p
     return real_path(Storage, dest, false);
   }
 
-  int fd;
-  std::error_code EC = openFileForRead(path, fd, &dest);
-
-  if (EC)
-    return EC;
-  ::close(fd);
+  SmallString<128> Storage;
+  StringRef P = path.toNullTerminatedStringRef(Storage);
+  char Buffer[PATH_MAX];
+  if (::realpath(P.begin(), Buffer) == nullptr)
+    return std::error_code(errno, std::generic_category());
+  dest.append(Buffer, Buffer + strlen(Buffer));
   return std::error_code();
 }
 

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=322095&r1=322094&r2=322095&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Tue Jan  9 09:27:45 2018
@@ -564,6 +564,25 @@ TEST_F(FileSystemTest, RealPath) {
   ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
 }
 
+#ifdef LLVM_ON_UNIX
+TEST_F(FileSystemTest, RealPathNoReadPerm) {
+  SmallString<64> Expanded;
+
+  ASSERT_NO_ERROR(
+    fs::create_directories(Twine(TestDirectory) + "/noreadperm"));
+  ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/noreadperm"));
+
+  fs::setPermissions(Twine(TestDirectory) + "/noreadperm", fs::no_perms);
+  fs::setPermissions(Twine(TestDirectory) + "/noreadperm", fs::all_exe);
+
+  ASSERT_NO_ERROR(fs::real_path(Twine(TestDirectory) + "/noreadperm", Expanded,
+                                false));
+
+  ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/noreadperm"));
+}
+#endif
+
+
 TEST_F(FileSystemTest, TempFileKeepDiscard) {
   // We can keep then discard.
   auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");




More information about the llvm-commits mailing list