Skip to content

Commit

Permalink
Fix clang-tidy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rgacogne committed Mar 18, 2024
1 parent 07d4785 commit 114b879
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 73 deletions.
1 change: 1 addition & 0 deletions pdns/dnsdistdist/doh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class DOHAcceptContext

std::map<int, std::string> d_ocspResponses;
std::unique_ptr<OpenSSLTLSTicketKeysRing> d_ticketKeys{nullptr};
// NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
pdns::UniqueFilePtr d_keyLogFile{nullptr};
ClientState* d_cs{nullptr};
time_t d_ticketsKeyRotationDelay{0};
Expand Down
8 changes: 4 additions & 4 deletions pdns/dnspcap2protobuf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ try {

PcapPacketReader pr(argv[1]);

auto fp = pdns::UniqueFilePtr(fopen(argv[2], "w"));
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(fopen(argv[2], "w"));
if (!filePtr) {
cerr<<"Error opening output file "<<argv[2]<<": "<<stringerror()<<endl;
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -150,8 +150,8 @@ try {
}

uint16_t mlen = htons(pbBuffer.length());
fwrite(&mlen, 1, sizeof(mlen), fp.get());
fwrite(pbBuffer.c_str(), 1, pbBuffer.length(), fp.get());
fwrite(&mlen, 1, sizeof(mlen), filePtr.get());
fwrite(pbBuffer.c_str(), 1, pbBuffer.length(), filePtr.get());
}
}
catch (const std::exception& e) {
Expand Down
8 changes: 4 additions & 4 deletions pdns/dnssecinfra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ using namespace boost::assign;
std::unique_ptr<DNSCryptoKeyEngine> DNSCryptoKeyEngine::makeFromISCFile(DNSKEYRecordContent& drc, const char* fname)
{
string sline, isc;
auto fp = pdns::UniqueFilePtr(fopen(fname, "r"));
if(!fp) {
auto filePtr = pdns::UniqueFilePtr(fopen(fname, "r"));
if(!filePtr) {
throw runtime_error("Unable to read file '"+string(fname)+"' for generating DNS Private Key");
}

while(stringfgets(fp.get(), sline)) {
while(stringfgets(filePtr.get(), sline)) {
isc += sline;
}
fp.reset();
filePtr.reset();

auto dke = makeFromISCString(drc, isc);
auto checkKeyErrors = std::vector<std::string>{};
Expand Down
12 changes: 6 additions & 6 deletions pdns/dnstcpbench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,24 @@ try
std::vector<std::thread> workers;
workers.reserve(numworkers);

pdns::UniqueFilePtr fp{nullptr};
pdns::UniqueFilePtr filePtr{nullptr};
if (!g_vm.count("file")) {
fp = pdns::UniqueFilePtr(fdopen(0, "r"));
filePtr = pdns::UniqueFilePtr(fdopen(0, "r"));
}
else {
fp = pdns::UniqueFilePtr(fopen(g_vm["file"].as<string>().c_str(), "r"));
if (!fp) {
filePtr = pdns::UniqueFilePtr(fopen(g_vm["file"].as<string>().c_str(), "r"));
if (!filePtr) {
unixDie("Unable to open "+g_vm["file"].as<string>()+" for input");
}
}
pair<string, string> q;
string line;
while(stringfgets(fp.get(), line)) {
while(stringfgets(filePtr.get(), line)) {
boost::trim_right(line);
q=splitField(line, ' ');
g_queries.push_back(BenchQuery(q.first, DNSRecordContent::TypeToNumber(q.second)));
}
fp.reset();
filePtr.reset();

for (unsigned int n = 0; n < numworkers; ++n) {
workers.push_back(std::thread(worker));
Expand Down
42 changes: 21 additions & 21 deletions pdns/libssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,23 +471,23 @@ bool libssl_generate_ocsp_response(const std::string& certFile, const std::strin
{
const EVP_MD* rmd = EVP_sha256();

auto fp = pdns::UniqueFilePtr(fopen(certFile.c_str(), "r"));
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(fopen(certFile.c_str(), "r"));
if (!filePtr) {
throw std::runtime_error("Unable to open '" + certFile + "' when loading the certificate to generate an OCSP response");
}
auto cert = std::unique_ptr<X509, void(*)(X509*)>(PEM_read_X509_AUX(fp.get(), nullptr, nullptr, nullptr), X509_free);
auto cert = std::unique_ptr<X509, void(*)(X509*)>(PEM_read_X509_AUX(filePtr.get(), nullptr, nullptr, nullptr), X509_free);

fp = pdns::UniqueFilePtr(fopen(caCert.c_str(), "r"));
if (!fp) {
filePtr = pdns::UniqueFilePtr(fopen(caCert.c_str(), "r"));
if (!filePtr) {
throw std::runtime_error("Unable to open '" + caCert + "' when loading the issuer certificate to generate an OCSP response");
}
auto issuer = std::unique_ptr<X509, void(*)(X509*)>(PEM_read_X509_AUX(fp.get(), nullptr, nullptr, nullptr), X509_free);
fp = pdns::UniqueFilePtr(fopen(caKey.c_str(), "r"));
if (!fp) {
auto issuer = std::unique_ptr<X509, void(*)(X509*)>(PEM_read_X509_AUX(filePtr.get(), nullptr, nullptr, nullptr), X509_free);
filePtr = pdns::UniqueFilePtr(fopen(caKey.c_str(), "r"));
if (!filePtr) {
throw std::runtime_error("Unable to open '" + caKey + "' when loading the issuer key to generate an OCSP response");
}
auto issuerKey = std::unique_ptr<EVP_PKEY, void(*)(EVP_PKEY*)>(PEM_read_PrivateKey(fp.get(), nullptr, nullptr, nullptr), EVP_PKEY_free);
fp.reset();
auto issuerKey = std::unique_ptr<EVP_PKEY, void(*)(EVP_PKEY*)>(PEM_read_PrivateKey(filePtr.get(), nullptr, nullptr, nullptr), EVP_PKEY_free);
filePtr.reset();

auto bs = std::unique_ptr<OCSP_BASICRESP, void(*)(OCSP_BASICRESP*)>(OCSP_BASICRESP_new(), OCSP_BASICRESP_free);
auto thisupd = std::unique_ptr<ASN1_TIME, void(*)(ASN1_TIME*)>(X509_gmtime_adj(nullptr, 0), ASN1_TIME_free);
Expand Down Expand Up @@ -939,11 +939,11 @@ std::pair<std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>, std::vector<std::st
if (!pair.d_key) {
#if defined(HAVE_SSL_CTX_USE_CERT_AND_KEY)
// If no separate key is given, treat it as a pkcs12 file
auto fp = pdns::UniqueFilePtr(fopen(pair.d_cert.c_str(), "r"));
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(fopen(pair.d_cert.c_str(), "r"));
if (!filePtr) {
throw std::runtime_error("Unable to open file " + pair.d_cert);
}
auto p12 = std::unique_ptr<PKCS12, void(*)(PKCS12*)>(d2i_PKCS12_fp(fp.get(), nullptr), PKCS12_free);
auto p12 = std::unique_ptr<PKCS12, void(*)(PKCS12*)>(d2i_PKCS12_fp(filePtr.get(), nullptr), PKCS12_free);
if (!p12) {
throw std::runtime_error("Unable to open PKCS12 file " + pair.d_cert);
}
Expand Down Expand Up @@ -1040,13 +1040,13 @@ static void libssl_key_log_file_callback(const SSL* ssl, const char* line)
return;
}

auto fp = reinterpret_cast<FILE*>(SSL_CTX_get_ex_data(sslCtx, s_keyLogIndex));
if (fp == nullptr) {
auto filePtr = reinterpret_cast<FILE*>(SSL_CTX_get_ex_data(sslCtx, s_keyLogIndex));
if (filePtr == nullptr) {
return;
}

fprintf(fp, "%s\n", line);
fflush(fp);
fprintf(filePtr, "%s\n", line);
fflush(filePtr);
}
#endif /* HAVE_SSL_CTX_SET_KEYLOG_CALLBACK */

Expand All @@ -1057,17 +1057,17 @@ pdns::UniqueFilePtr libssl_set_key_log_file(std::unique_ptr<SSL_CTX, decltype(&S
if (fd == -1) {
unixDie("Error opening TLS log file '" + logFile + "'");
}
auto fp = pdns::UniqueFilePtr(fdopen(fd, "a"));
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(fdopen(fd, "a"));
if (!filePtr) {
int error = errno; // close might clobber errno
close(fd);
throw std::runtime_error("Error opening TLS log file '" + logFile + "': " + stringerror(error));
}

SSL_CTX_set_ex_data(ctx.get(), s_keyLogIndex, fp.get());
SSL_CTX_set_ex_data(ctx.get(), s_keyLogIndex, filePtr.get());
SSL_CTX_set_keylog_callback(ctx.get(), &libssl_key_log_file_callback);

return fp;
return filePtr;
#else
return pdns::UniqueFilePtr(nullptr);
#endif /* HAVE_SSL_CTX_SET_KEYLOG_CALLBACK */
Expand Down
6 changes: 3 additions & 3 deletions pdns/misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -861,11 +861,11 @@ bool stringfgets(FILE* fp, std::string& line)
bool readFileIfThere(const char* fname, std::string* line)
{
line->clear();
auto fp = pdns::UniqueFilePtr(fopen(fname, "r"));
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(fopen(fname, "r"));
if (!filePtr) {
return false;
}
return stringfgets(fp.get(), *line);
return stringfgets(filePtr.get(), *line);
}

Regex::Regex(const string &expr)
Expand Down
6 changes: 3 additions & 3 deletions pdns/pdnsutil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3527,14 +3527,14 @@ try
const auto algorithm = pdns::checked_stoi<unsigned int>(cmds.at(3));

errno = 0;
pdns::UniqueFilePtr fp{std::fopen(filename.c_str(), "r")};
if (fp == nullptr) {
pdns::UniqueFilePtr filePtr{std::fopen(filename.c_str(), "r")};
if (filePtr == nullptr) {
auto errMsg = pdns::getMessageFromErrno(errno);
throw runtime_error("Failed to open PEM file `" + filename + "`: " + errMsg);
}

DNSKEYRecordContent drc;
shared_ptr<DNSCryptoKeyEngine> key{DNSCryptoKeyEngine::makeFromPEMFile(drc, algorithm, *fp, filename)};
shared_ptr<DNSCryptoKeyEngine> key{DNSCryptoKeyEngine::makeFromPEMFile(drc, algorithm, *filePtr, filename)};
if (!key) {
cerr << "Could not convert key from PEM to internal format" << endl;
return 1;
Expand Down
14 changes: 7 additions & 7 deletions pdns/recursordist/aggressive_nsec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -914,33 +914,33 @@ bool AggressiveNSECCache::getDenial(time_t now, const DNSName& name, const QType
return true;
}

size_t AggressiveNSECCache::dumpToFile(pdns::UniqueFilePtr& fp, const struct timeval& now)
size_t AggressiveNSECCache::dumpToFile(pdns::UniqueFilePtr& filePtr, const struct timeval& now)
{
size_t ret = 0;

auto zones = d_zones.read_lock();
zones->visit([&ret, now, &fp](const SuffixMatchTree<std::shared_ptr<LockGuarded<ZoneEntry>>>& node) {
zones->visit([&ret, now, &filePtr](const SuffixMatchTree<std::shared_ptr<LockGuarded<ZoneEntry>>>& node) {
if (!node.d_value) {
return;
}

auto zone = node.d_value->lock();
fprintf(fp.get(), "; Zone %s\n", zone->d_zone.toString().c_str());
fprintf(filePtr.get(), "; Zone %s\n", zone->d_zone.toString().c_str());

for (const auto& entry : zone->d_entries) {
int64_t ttl = entry.d_ttd - now.tv_sec;
try {
fprintf(fp.get(), "%s %" PRId64 " IN %s %s\n", entry.d_owner.toString().c_str(), ttl, zone->d_nsec3 ? "NSEC3" : "NSEC", entry.d_record->getZoneRepresentation().c_str());
fprintf(filePtr.get(), "%s %" PRId64 " IN %s %s\n", entry.d_owner.toString().c_str(), ttl, zone->d_nsec3 ? "NSEC3" : "NSEC", entry.d_record->getZoneRepresentation().c_str());
for (const auto& signature : entry.d_signatures) {
fprintf(fp.get(), "- RRSIG %s\n", signature->getZoneRepresentation().c_str());
fprintf(filePtr.get(), "- RRSIG %s\n", signature->getZoneRepresentation().c_str());
}
++ret;
}
catch (const std::exception& e) {
fprintf(fp.get(), "; Error dumping record from zone %s: %s\n", zone->d_zone.toString().c_str(), e.what());
fprintf(filePtr.get(), "; Error dumping record from zone %s: %s\n", zone->d_zone.toString().c_str(), e.what());
}
catch (...) {
fprintf(fp.get(), "; Error dumping record from zone %s\n", zone->d_zone.toString().c_str());
fprintf(filePtr.get(), "; Error dumping record from zone %s\n", zone->d_zone.toString().c_str());
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion pdns/recursordist/aggressive_nsec.hh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public:
static bool isSmallCoveringNSEC3(const DNSName& owner, const std::string& nextHash);

void prune(time_t now);
size_t dumpToFile(pdns::UniqueFilePtr& fp, const struct timeval& now);
size_t dumpToFile(pdns::UniqueFilePtr& filePtr, const struct timeval& now);

private:
struct ZoneEntry
Expand Down
24 changes: 12 additions & 12 deletions pdns/recursordist/negcache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,22 @@ void NegCache::prune(time_t now, size_t maxEntries)
}

/*!
* Writes the whole negative cache to fp
* Writes the whole negative cache to fd
*
* \param fp A pointer to an open FILE object
* \param fd A pointer to an open FILE object
*/
size_t NegCache::doDump(int fd, size_t maxCacheEntries, time_t now)
{
int newfd = dup(fd);
if (newfd == -1) {
return 0;
}
auto fp = pdns::UniqueFilePtr(fdopen(newfd, "w"));
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(fdopen(newfd, "w"));
if (!filePtr) {
close(newfd);
return 0;
}
fprintf(fp.get(), "; negcache dump follows\n;\n");
fprintf(filePtr.get(), "; negcache dump follows\n;\n");

size_t ret = 0;

Expand All @@ -301,29 +301,29 @@ size_t NegCache::doDump(int fd, size_t maxCacheEntries, time_t now)
for (auto& mc : d_maps) {
auto m = mc.lock();
const auto shardSize = m->d_map.size();
fprintf(fp.get(), "; negcache shard %zu; size %zu\n", shard, shardSize);
fprintf(filePtr.get(), "; negcache shard %zu; size %zu\n", shard, shardSize);
min = std::min(min, shardSize);
max = std::max(max, shardSize);
shard++;
auto& sidx = m->d_map.get<SequenceTag>();
for (const NegCacheEntry& ne : sidx) {
ret++;
int64_t ttl = ne.d_ttd - now;
fprintf(fp.get(), "%s %" PRId64 " IN %s VIA %s ; (%s) origttl=%" PRIu32 " ss=%hu\n", ne.d_name.toString().c_str(), ttl, ne.d_qtype.toString().c_str(), ne.d_auth.toString().c_str(), vStateToString(ne.d_validationState).c_str(), ne.d_orig_ttl, ne.d_servedStale);
fprintf(filePtr.get(), "%s %" PRId64 " IN %s VIA %s ; (%s) origttl=%" PRIu32 " ss=%hu\n", ne.d_name.toString().c_str(), ttl, ne.d_qtype.toString().c_str(), ne.d_auth.toString().c_str(), vStateToString(ne.d_validationState).c_str(), ne.d_orig_ttl, ne.d_servedStale);
for (const auto& rec : ne.authoritySOA.records) {
fprintf(fp.get(), "%s %" PRId64 " IN %s %s ; (%s)\n", rec.d_name.toString().c_str(), ttl, DNSRecordContent::NumberToType(rec.d_type).c_str(), rec.getContent()->getZoneRepresentation().c_str(), vStateToString(ne.d_validationState).c_str());
fprintf(filePtr.get(), "%s %" PRId64 " IN %s %s ; (%s)\n", rec.d_name.toString().c_str(), ttl, DNSRecordContent::NumberToType(rec.d_type).c_str(), rec.getContent()->getZoneRepresentation().c_str(), vStateToString(ne.d_validationState).c_str());
}
for (const auto& sig : ne.authoritySOA.signatures) {
fprintf(fp.get(), "%s %" PRId64 " IN RRSIG %s ;\n", sig.d_name.toString().c_str(), ttl, sig.getContent()->getZoneRepresentation().c_str());
fprintf(filePtr.get(), "%s %" PRId64 " IN RRSIG %s ;\n", sig.d_name.toString().c_str(), ttl, sig.getContent()->getZoneRepresentation().c_str());
}
for (const auto& rec : ne.DNSSECRecords.records) {
fprintf(fp.get(), "%s %" PRId64 " IN %s %s ; (%s)\n", rec.d_name.toString().c_str(), ttl, DNSRecordContent::NumberToType(rec.d_type).c_str(), rec.getContent()->getZoneRepresentation().c_str(), vStateToString(ne.d_validationState).c_str());
fprintf(filePtr.get(), "%s %" PRId64 " IN %s %s ; (%s)\n", rec.d_name.toString().c_str(), ttl, DNSRecordContent::NumberToType(rec.d_type).c_str(), rec.getContent()->getZoneRepresentation().c_str(), vStateToString(ne.d_validationState).c_str());
}
for (const auto& sig : ne.DNSSECRecords.signatures) {
fprintf(fp.get(), "%s %" PRId64 " IN RRSIG %s ;\n", sig.d_name.toString().c_str(), ttl, sig.getContent()->getZoneRepresentation().c_str());
fprintf(filePtr.get(), "%s %" PRId64 " IN RRSIG %s ;\n", sig.d_name.toString().c_str(), ttl, sig.getContent()->getZoneRepresentation().c_str());
}
}
}
fprintf(fp.get(), "; negcache size: %zu/%zu shards: %zu min/max shard size: %zu/%zu\n", size(), maxCacheEntries, d_maps.size(), min, max);
fprintf(filePtr.get(), "; negcache size: %zu/%zu shards: %zu min/max shard size: %zu/%zu\n", size(), maxCacheEntries, d_maps.size(), min, max);
return ret;
}
14 changes: 7 additions & 7 deletions pdns/recursordist/rec_channel_rec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,15 @@ static uint64_t dumpAggressiveNSECCache(int fd)
if (newfd == -1) {
return 0;
}
auto fp = pdns::UniqueFilePtr(fdopen(newfd, "w"));
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(fdopen(newfd, "w"));
if (!filePtr) {
return 0;
}
fprintf(fp.get(), "; aggressive NSEC cache dump follows\n;\n");
fprintf(filePtr.get(), "; aggressive NSEC cache dump follows\n;\n");

struct timeval now;
Utility::gettimeofday(&now, nullptr);
return g_aggressiveNSECCache->dumpToFile(fp, now);
return g_aggressiveNSECCache->dumpToFile(filePtr, now);
}

static uint64_t* pleaseDumpEDNSMap(int fd)
Expand Down Expand Up @@ -480,13 +480,13 @@ static RecursorControlChannel::Answer doDumpRPZ(int s, T begin, T end)
return {1, "No RPZ zone named " + zoneName + "\n"};
}

auto fp = pdns::UniqueFilePtr(fdopen(fdw, "w"));
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(fdopen(fdw, "w"));
if (!filePtr) {
int err = errno;
return {1, "converting file descriptor: " + stringerror(err) + "\n"};
}

zone->dump(fp.get());
zone->dump(filePtr.get());

return {0, "done\n"};
}
Expand Down
10 changes: 5 additions & 5 deletions pdns/recursordist/test-negcache_cc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,20 +525,20 @@ BOOST_AUTO_TEST_CASE(test_dumpToFile)
cache.add(genNegCacheEntry(DNSName("www1.powerdns.com"), DNSName("powerdns.com"), now));
cache.add(genNegCacheEntry(DNSName("www2.powerdns.com"), DNSName("powerdns.com"), now));

auto fp = pdns::UniqueFilePtr(tmpfile());
if (!fp) {
auto filePtr = pdns::UniqueFilePtr(tmpfile());
if (!filePtr) {
BOOST_FAIL("Temporary file could not be opened");
}

cache.doDump(fileno(fp.get()), 0, now.tv_sec);
cache.doDump(fileno(filePtr.get()), 0, now.tv_sec);

rewind(fp.get());
rewind(filePtr.get());
char* line = nullptr;
size_t len = 0;
ssize_t read;

for (auto str : expected) {
read = getline(&line, &len, fp.get());
read = getline(&line, &len, filePtr.get());
if (read == -1)
BOOST_FAIL("Unable to read a line from the temp file");
// The clock might have ticked so the 600 becomes 599
Expand Down

0 comments on commit 114b879

Please sign in to comment.