Skip to content

Commit 771a1d8

Browse files
authored
Merge pull request llvm#1236 from apple/🍒/d144087c963d8189bb4aeaa7800dcb9f93ac84db
[lldb/Support] Treat empty FileSpec as an invalid file.
2 parents dd0d195 + c24629f commit 771a1d8

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

lldb/source/Host/common/FileSystem.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ Optional<FileSystem> &FileSystem::InstanceImpl() {
8787

8888
vfs::directory_iterator FileSystem::DirBegin(const FileSpec &file_spec,
8989
std::error_code &ec) {
90+
if (!file_spec) {
91+
ec = std::error_code(static_cast<int>(errc::no_such_file_or_directory),
92+
std::system_category());
93+
return {};
94+
}
9095
return DirBegin(file_spec.GetPath(), ec);
9196
}
9297

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

98103
llvm::ErrorOr<vfs::Status>
99104
FileSystem::GetStatus(const FileSpec &file_spec) const {
105+
if (!file_spec)
106+
return std::error_code(static_cast<int>(errc::no_such_file_or_directory),
107+
std::system_category());
100108
return GetStatus(file_spec.GetPath());
101109
}
102110

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

107115
sys::TimePoint<>
108116
FileSystem::GetModificationTime(const FileSpec &file_spec) const {
117+
if (!file_spec)
118+
return sys::TimePoint<>();
109119
return GetModificationTime(file_spec.GetPath());
110120
}
111121

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

119129
uint64_t FileSystem::GetByteSize(const FileSpec &file_spec) const {
130+
if (!file_spec)
131+
return 0;
120132
return GetByteSize(file_spec.GetPath());
121133
}
122134

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

134146
uint32_t FileSystem::GetPermissions(const FileSpec &file_spec,
135147
std::error_code &ec) const {
148+
if (!file_spec)
149+
return sys::fs::perms::perms_not_known;
136150
return GetPermissions(file_spec.GetPath(), ec);
137151
}
138152

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

156170
bool FileSystem::Exists(const FileSpec &file_spec) const {
157-
return Exists(file_spec.GetPath());
171+
return file_spec && Exists(file_spec.GetPath());
158172
}
159173

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

164178
bool FileSystem::Readable(const FileSpec &file_spec) const {
165-
return Readable(file_spec.GetPath());
179+
return file_spec && Readable(file_spec.GetPath());
166180
}
167181

168182
bool FileSystem::IsDirectory(const Twine &path) const {
@@ -173,7 +187,7 @@ bool FileSystem::IsDirectory(const Twine &path) const {
173187
}
174188

175189
bool FileSystem::IsDirectory(const FileSpec &file_spec) const {
176-
return IsDirectory(file_spec.GetPath());
190+
return file_spec && IsDirectory(file_spec.GetPath());
177191
}
178192

179193
bool FileSystem::IsLocal(const Twine &path) const {
@@ -183,7 +197,7 @@ bool FileSystem::IsLocal(const Twine &path) const {
183197
}
184198

185199
bool FileSystem::IsLocal(const FileSpec &file_spec) const {
186-
return IsLocal(file_spec.GetPath());
200+
return file_spec && IsLocal(file_spec.GetPath());
187201
}
188202

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

263277
void FileSystem::Resolve(FileSpec &file_spec) {
278+
if (!file_spec)
279+
return;
280+
264281
// Extract path from the FileSpec.
265282
SmallString<128> path;
266283
file_spec.GetPath(path);

lldb/unittests/Host/FileSystemTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,29 @@ TEST(FileSystemTest, OpenErrno) {
303303
EXPECT_EQ(code.value(), ENOENT);
304304
}
305305

306+
TEST(FileSystemTest, EmptyTest) {
307+
FileSpec spec;
308+
FileSystem fs;
309+
310+
{
311+
std::error_code ec;
312+
fs.DirBegin(spec, ec);
313+
EXPECT_EQ(ec.category(), std::system_category());
314+
EXPECT_EQ(ec.value(), ENOENT);
315+
}
316+
317+
{
318+
llvm::ErrorOr<vfs::Status> status = fs.GetStatus(spec);
319+
ASSERT_FALSE(status);
320+
EXPECT_EQ(status.getError().category(), std::system_category());
321+
EXPECT_EQ(status.getError().value(), ENOENT);
322+
}
323+
324+
EXPECT_EQ(sys::TimePoint<>(), fs.GetModificationTime(spec));
325+
EXPECT_EQ(static_cast<uint64_t>(0), fs.GetByteSize(spec));
326+
EXPECT_EQ(llvm::sys::fs::perms::perms_not_known, fs.GetPermissions(spec));
327+
EXPECT_FALSE(fs.Exists(spec));
328+
EXPECT_FALSE(fs.Readable(spec));
329+
EXPECT_FALSE(fs.IsDirectory(spec));
330+
EXPECT_FALSE(fs.IsLocal(spec));
331+
}

0 commit comments

Comments
 (0)