From: Remi Gacogne Date: Tue, 29 Aug 2023 12:57:19 +0000 (+0200) Subject: Wrap DIR* objects in unique pointers to prevent memory leaks X-Git-Tag: rec-5.0.0-rc1~46^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84a873c7273e09263386de61ba11ffded0fc39d4;p=thirdparty%2Fpdns.git Wrap DIR* objects in unique pointers to prevent memory leaks --- diff --git a/modules/geoipbackend/geoipbackend.cc b/modules/geoipbackend/geoipbackend.cc index 783c421e83..681bf6d926 100644 --- a/modules/geoipbackend/geoipbackend.cc +++ b/modules/geoipbackend/geoipbackend.cc @@ -85,12 +85,12 @@ GeoIPBackend::GeoIPBackend(const string& suffix) WriteLock writeLock(&s_state_lock); setArgPrefix("geoip" + suffix); if (!getArg("dnssec-keydir").empty()) { - DIR* dir = opendir(getArg("dnssec-keydir").c_str()); - if (dir == nullptr) { + auto dirHandle = std::unique_ptr(opendir(getArg("dnssec-keydir").c_str()), closedir); + if (!dirHandle) { throw PDNSException("dnssec-keydir " + getArg("dnssec-keydir") + " does not exist"); } d_dnssec = true; - closedir(dir); + dirHandle.reset(); } if (s_rc == 0) { // first instance gets to open everything initialize(); diff --git a/pdns/dnsbackend.cc b/pdns/dnsbackend.cc index 8cceece213..71b05ff785 100644 --- a/pdns/dnsbackend.cc +++ b/pdns/dnsbackend.cc @@ -113,19 +113,18 @@ vector BackendMakerClass::getModules() void BackendMakerClass::load_all() { // TODO: Implement this? - DIR *dir=opendir(arg()["module-dir"].c_str()); - if(!dir) { + auto dir = std::unique_ptr(opendir(arg()["module-dir"].c_str()), closedir); + if (!dir) { g_log<d_name,"lib",3) && - strlen(entry->d_name)>13 && - !strcmp(entry->d_name+strlen(entry->d_name)-10,"backend.so")) + struct dirent* entry = nullptr; + while ((entry=readdir(dir.get()))) { + if (strncmp(entry->d_name, "lib", 3) == 0 && + strlen(entry->d_name) > 13 && + strcmp(entry->d_name + strlen(entry->d_name)-10, "backend.so") == 0) load(entry->d_name); } - closedir(dir); } void BackendMakerClass::load(const string &module) diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 78da52ec9a..78490d7560 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -1861,16 +1861,16 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) return; } - DIR* dirp; - struct dirent* ent; + auto dirp = std::unique_ptr(opendir(dirname.c_str()), closedir); std::vector files; - if (!(dirp = opendir(dirname.c_str()))) { + if (!dirp) { errlog("Error opening the included directory %s!", dirname.c_str()); g_outputBuffer = "Error opening the included directory " + dirname + "!"; return; } - while ((ent = readdir(dirp)) != NULL) { + struct dirent* ent = nullptr; + while ((ent = readdir(dirp.get())) != NULL) { if (ent->d_name[0] == '.') { continue; } @@ -1887,7 +1887,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } } - closedir(dirp); + dirp.reset(); std::sort(files.begin(), files.end()); g_included = true; diff --git a/pdns/ixfrdist.cc b/pdns/ixfrdist.cc index 9a36a3c84b..7192cd9958 100644 --- a/pdns/ixfrdist.cc +++ b/pdns/ixfrdist.cc @@ -202,20 +202,19 @@ static bool sortSOA(uint32_t i, uint32_t j) { static void cleanUpDomain(const DNSName& domain, const uint16_t& keep, const string& workdir) { string dir = workdir + "/" + domain.toString(); - DIR *dp; - dp = opendir(dir.c_str()); - if (dp == nullptr) { + auto dirHandle = std::unique_ptr(opendir(dir.c_str()), closedir); + if (!dirHandle) { return; } vector zoneVersions; - struct dirent *d; - while ((d = readdir(dp)) != nullptr) { - if(!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) { + struct dirent* entry = nullptr; + while ((entry = readdir(dirHandle.get())) != nullptr) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } - zoneVersions.push_back(std::stoi(d->d_name)); + zoneVersions.push_back(std::stoi(entry->d_name)); } - closedir(dp); + dirHandle.reset(); g_log<(opendir(dir.c_str()), closedir); + if (!dirhdl) { throw runtime_error("Could not open IXFR directory '" + dir + "': " + stringerror()); - struct dirent *entry; + } - while((entry = readdir(dirhdl))) { + struct dirent* entry = nullptr; + while ((entry = readdir(dirhdl.get()))) { uint32_t num = atoi(entry->d_name); - if(std::to_string(num) == entry->d_name) + if (std::to_string(num) == entry->d_name) { ret = max(num, ret); + } } - closedir(dirhdl); + return ret; } diff --git a/pdns/misc.cc b/pdns/misc.cc index 4bbc865f6b..3cb1275d84 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -1377,23 +1377,24 @@ DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum) uint64_t getOpenFileDescriptors(const std::string&) { #ifdef __linux__ - DIR* dirhdl=opendir(("/proc/"+std::to_string(getpid())+"/fd/").c_str()); - if(!dirhdl) + auto dirhdl = std::unique_ptr(opendir(("/proc/"+std::to_string(getpid())+"/fd/").c_str()), closedir); + if (!dirhdl) { return 0; + } - struct dirent *entry; - int ret=0; - while((entry = readdir(dirhdl))) { + int ret = 0; + struct dirent* entry = nullptr; + while ((entry = readdir(dirhdl.get()))) { uint32_t num; try { pdns::checked_stoi_into(num, entry->d_name); } catch (...) { continue; // was not a number. } - if(std::to_string(num) == entry->d_name) + if (std::to_string(num) == entry->d_name) { ret++; + } } - closedir(dirhdl); return ret; #elif defined(__OpenBSD__)