Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldb/Support] Treat empty FileSpec as an invalid file. #1236

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lldb/source/Host/common/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ Optional<FileSystem> &FileSystem::InstanceImpl() {

vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
std::error_code &ec) {
if (!file_spec) {
ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory),
std::system_category());
return {};
}
return DirBegin(file_spec.GetPath(), ec);
}

Expand All @@ -97,6 +102,9 @@ vfs::directory_iterator FileSystem::DirBegin(const Twine &dir,

llvm::ErrorOr<vfs::Status>
FileSystem::GetStatus(const FileSpec &file_spec) const {
if (!file_spec)
return std::error_code(static_cast<int>(errc::no_such_file_or_directory),
std::system_category());
return GetStatus(file_spec.GetPath());
}

Expand All @@ -106,6 +114,8 @@ llvm::ErrorOr<vfs::Status> FileSystem::GetStatus(const Twine &path) const {

sys::TimePoint<>
FileSystem::GetModificationTime(const FileSpec &file_spec) const {
if (!file_spec)
return sys::TimePoint<>();
return GetModificationTime(file_spec.GetPath());
}

Expand All @@ -117,6 +127,8 @@ sys::TimePoint<> FileSystem::GetModificationTime(const Twine &path) const {
}

uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
if (!file_spec)
return 0;
return GetByteSize(file_spec.GetPath());
}

Expand All @@ -133,6 +145,8 @@ uint32_t FileSystem::GetPermissions(const FileSpec &file_spec) const {

uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
std::error_code &ec) const {
if (!file_spec)
return sys::fs::perms::perms_not_known;
return GetPermissions(file_spec.GetPath(), ec);
}

Expand All @@ -154,15 +168,15 @@ uint32_t FileSystem::GetPermissions(const Twine &path,
bool FileSystem::Exists(const Twine &path) const { return m_fs->exists(path); }

bool FileSystem::Exists(const FileSpec &file_spec) const {
return Exists(file_spec.GetPath());
return file_spec && Exists(file_spec.GetPath());
}

bool FileSystem::Readable(const Twine &path) const {
return GetPermissions(path) & sys::fs::perms::all_read;
}

bool FileSystem::Readable(const FileSpec &file_spec) const {
return Readable(file_spec.GetPath());
return file_spec && Readable(file_spec.GetPath());
}

bool FileSystem::IsDirectory(const Twine &path) const {
Expand All @@ -173,7 +187,7 @@ bool FileSystem::IsDirectory(const Twine &path) const {
}

bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
return IsDirectory(file_spec.GetPath());
return file_spec && IsDirectory(file_spec.GetPath());
}

bool FileSystem::IsLocal(const Twine &path) const {
Expand All @@ -183,7 +197,7 @@ bool FileSystem::IsLocal(const Twine &path) const {
}

bool FileSystem::IsLocal(const FileSpec &file_spec) const {
return IsLocal(file_spec.GetPath());
return file_spec && IsLocal(file_spec.GetPath());
}

void FileSystem::EnumerateDirectory(Twine path, bool find_directories,
Expand Down Expand Up @@ -261,6 +275,9 @@ void FileSystem::Resolve(SmallVectorImpl<char> &path) {
}

void FileSystem::Resolve(FileSpec &file_spec) {
if (!file_spec)
return;

// Extract path from the FileSpec.
SmallString<128> path;
file_spec.GetPath(path);
Expand Down
26 changes: 26 additions & 0 deletions lldb/unittests/Host/FileSystemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,29 @@ TEST(FileSystemTest, OpenErrno) {
EXPECT_EQ(code.value(), ENOENT);
}

TEST(FileSystemTest, EmptyTest) {
FileSpec spec;
FileSystem fs;

{
std::error_code ec;
fs.DirBegin(spec, ec);
EXPECT_EQ(ec.category(), std::system_category());
EXPECT_EQ(ec.value(), ENOENT);
}

{
llvm::ErrorOr<vfs::Status> status = fs.GetStatus(spec);
ASSERT_FALSE(status);
EXPECT_EQ(status.getError().category(), std::system_category());
EXPECT_EQ(status.getError().value(), ENOENT);
}

EXPECT_EQ(sys::TimePoint<>(), fs.GetModificationTime(spec));
EXPECT_EQ(static_cast<uint64_t>(0), fs.GetByteSize(spec));
EXPECT_EQ(llvm::sys::fs::perms::perms_not_known, fs.GetPermissions(spec));
EXPECT_FALSE(fs.Exists(spec));
EXPECT_FALSE(fs.Readable(spec));
EXPECT_FALSE(fs.IsDirectory(spec));
EXPECT_FALSE(fs.IsLocal(spec));
}