diff options
| author | Davide Italiano <davide@freebsd.org> | 2018-01-09 17:27:45 +0000 |
|---|---|---|
| committer | Davide Italiano <davide@freebsd.org> | 2018-01-09 17:27:45 +0000 |
| commit | 4762c069dead49ba96c05da0b827f52fad117164 (patch) | |
| tree | 578bb4e32df52e54a7c7ab0be29c2c3e55eb1078 | |
| parent | 9aaf5d3e717e14d0a06fd6598c988537791e55b9 (diff) | |
| download | bcm5719-llvm-4762c069dead49ba96c05da0b827f52fad117164.tar.gz bcm5719-llvm-4762c069dead49ba96c05da0b827f52fad117164.zip | |
[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>
llvm-svn: 322095
| -rw-r--r-- | llvm/lib/Support/Unix/Path.inc | 12 | ||||
| -rw-r--r-- | llvm/unittests/Support/Path.cpp | 19 |
2 files changed, 25 insertions, 6 deletions
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc index 2ecb97316c8..220162d1c19 100644 --- a/llvm/lib/Support/Unix/Path.inc +++ b/llvm/lib/Support/Unix/Path.inc @@ -860,12 +860,12 @@ std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest, 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(); } diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index f624626f5e5..f30ef69b565 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -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-%%%%"); |

