Skip to content

Commit f03fdc6

Browse files
committed
Clean up readdir code, add a comment about why we're using readdir
1 parent 0e4ad51 commit f03fdc6

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

util-unix.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,22 @@ std::vector<std::string> get_directory_contents (const char* path)
188188
throw System_error("opendir", path, errno);
189189
}
190190
try {
191-
struct dirent* ent = NULL;
192-
193191
errno = 0;
194-
195-
while((ent = readdir(dir)) != NULL && errno == 0) {
196-
if (std::strcmp(ent->d_name, ".") && std::strcmp(ent->d_name, ".."))
192+
// Note: readdir is reentrant in new implementations. In old implementations,
193+
// it might not be, but git-crypt isn't multi-threaded so that's OK.
194+
// We don't use readdir_r because it's buggy and deprecated:
195+
// https://womble.decadent.org.uk/readdir_r-advisory.html
196+
// http://austingroupbugs.net/view.php?id=696
197+
// http://man7.org/linux/man-pages/man3/readdir_r.3.html
198+
while (struct dirent* ent = readdir(dir)) {
199+
if (!(std::strcmp(ent->d_name, ".") == 0 || std::strcmp(ent->d_name, "..") == 0)) {
197200
contents.push_back(ent->d_name);
201+
}
198202
}
199203

200-
if(errno)
204+
if (errno) {
201205
throw System_error("readdir", path, errno);
206+
}
202207

203208
} catch (...) {
204209
closedir(dir);

0 commit comments

Comments
 (0)