From 50ef17f43df607fbf5e64c9418ea0fcc5d355ea4 Mon Sep 17 00:00:00 2001 From: Martin Conte Mac Donell Date: Sat, 4 Feb 2017 00:58:26 -0800 Subject: [PATCH] Remove `readdir_r` in favor of `readdir` `readdir_r` has been considered harmful for a while now and it's deprecated by glibc since 2.24. I know `readdir_r` was historically the thread-safe alternative but even if POSIX don't require `readdir` to be thread-safe, it is thread-safe in any modern implementation such as glibc. Note that the docs says that the next POSIX spec will require `readdir` to be thread-safe "officially". --- source/common/runtime/runtime_impl.cc | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/source/common/runtime/runtime_impl.cc b/source/common/runtime/runtime_impl.cc index b31e648d5444d..bfa5cae5fb6d0 100644 --- a/source/common/runtime/runtime_impl.cc +++ b/source/common/runtime/runtime_impl.cc @@ -75,30 +75,29 @@ uint64_t SnapshotImpl::getInteger(const std::string& key, uint64_t default_value void SnapshotImpl::walkDirectory(const std::string& path, const std::string& prefix) { log_debug("walking directory: {}", path); Directory current_dir(path); - dirent entry; - dirent* result; while (true) { - int rc = readdir_r(current_dir.dir_, &entry, &result); - if (0 != rc) { + errno = 0; + dirent* entry = readdir(current_dir.dir_); + if (entry == nullptr && errno != 0) { throw EnvoyException(fmt::format("unable to iterate directory: {}", path)); } - if (!result) { + if (entry == nullptr) { break; } - std::string full_path = path + "/" + entry.d_name; + std::string full_path = path + "/" + entry->d_name; std::string full_prefix; if (prefix.empty()) { - full_prefix = entry.d_name; + full_prefix = entry->d_name; } else { - full_prefix = prefix + "." + entry.d_name; + full_prefix = prefix + "." + entry->d_name; } - if (entry.d_type == DT_DIR && std::string(entry.d_name) != "." && - std::string(entry.d_name) != "..") { + if (entry->d_type == DT_DIR && std::string(entry->d_name) != "." && + std::string(entry->d_name) != "..") { walkDirectory(full_path, full_prefix); - } else if (entry.d_type == DT_REG) { + } else if (entry->d_type == DT_REG) { // Suck the file into a string. This is not very efficient but it should be good enough // for small files. Also, as noted elsewhere, none of this is non-blocking which could // theoretically lead to issues.