aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/FileManager.cpp
diff options
context:
space:
mode:
authorSean Silva <chisophugis@gmail.com>2015-07-30 00:26:34 +0000
committerSean Silva <chisophugis@gmail.com>2015-07-30 00:26:34 +0000
commitb963dedbe87ba04fafb9433bbc345dbd1c3391ee (patch)
tree8702834c1497491371333f51f82c9bea45175475 /clang/lib/Basic/FileManager.cpp
parent5bfbb36a09fac45bd0230fceb42a58e3c1977620 (diff)
downloadllvm-b963dedbe87ba04fafb9433bbc345dbd1c3391ee.zip
llvm-b963dedbe87ba04fafb9433bbc345dbd1c3391ee.tar.gz
llvm-b963dedbe87ba04fafb9433bbc345dbd1c3391ee.tar.bz2
Avoid failure to canonicalize '..'.
Also fix completely broken and untested code which was hiding the primary bug. The !LLVM_ON_UNIX branch of the ifdef was actually a no-op. I ran into this in the wild. It was causing failures in our SDK build. Ideally we'd have a perfect llvm::sys::fs::canonical, but at least this is a step in the right direction, and fixes an obviously broken case. In some sense the test case I've added here is an integration test. We should have these routines thoroughly unit tested in llvm::sys::fs. llvm-svn: 243597
Diffstat (limited to 'clang/lib/Basic/FileManager.cpp')
-rw-r--r--clang/lib/Basic/FileManager.cpp19
1 files changed, 11 insertions, 8 deletions
diff --git a/clang/lib/Basic/FileManager.cpp b/clang/lib/Basic/FileManager.cpp
index d492744..3d16056 100644
--- a/clang/lib/Basic/FileManager.cpp
+++ b/clang/lib/Basic/FileManager.cpp
@@ -514,7 +514,7 @@ void FileManager::modifyFileEntry(FileEntry *File,
File->ModTime = ModificationTime;
}
-/// Remove '.' path components from the given absolute path.
+/// Remove '.' and '..' path components from the given absolute path.
/// \return \c true if any changes were made.
// FIXME: Move this to llvm::sys::path.
bool FileManager::removeDotPaths(SmallVectorImpl<char> &Path) {
@@ -525,24 +525,24 @@ bool FileManager::removeDotPaths(SmallVectorImpl<char> &Path) {
// Skip the root path, then look for traversal in the components.
StringRef Rel = path::relative_path(P);
- bool AnyDots = false;
for (StringRef C : llvm::make_range(path::begin(Rel), path::end(Rel))) {
- if (C == ".") {
- AnyDots = true;
+ if (C == ".")
+ continue;
+ if (C == "..") {
+ if (!ComponentStack.empty())
+ ComponentStack.pop_back();
continue;
}
ComponentStack.push_back(C);
}
- if (!AnyDots)
- return false;
-
SmallString<256> Buffer = path::root_path(P);
for (StringRef C : ComponentStack)
path::append(Buffer, C);
+ bool Changed = (Path != Buffer);
Path.swap(Buffer);
- return true;
+ return Changed;
}
StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
@@ -567,6 +567,9 @@ StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
llvm::sys::fs::make_absolute(CanonicalNameBuf);
llvm::sys::path::native(CanonicalNameBuf);
removeDotPaths(CanonicalNameBuf);
+ char *Mem = CanonicalNameStorage.Allocate<char>(CanonicalNameBuf.size());
+ memcpy(Mem, CanonicalNameBuf.data(), CanonicalNameBuf.size());
+ CanonicalName = StringRef(Mem, CanonicalNameBuf.size());
#endif
CanonicalDirNames.insert(std::make_pair(Dir, CanonicalName));