From 99934d024e29fb7dc8d760998ad595abc59e1bb9 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Tue, 28 Mar 2023 16:32:41 +0200 Subject: [PATCH] rec: fix a race in the negcache unit tests --- pdns/recursordist/negcache.cc | 7 ++----- pdns/recursordist/negcache.hh | 2 +- pdns/recursordist/test-negcache_cc.cc | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/pdns/recursordist/negcache.cc b/pdns/recursordist/negcache.cc index 77cc3e3b8d..57ac811e13 100644 --- a/pdns/recursordist/negcache.cc +++ b/pdns/recursordist/negcache.cc @@ -295,7 +295,7 @@ void NegCache::prune(size_t maxEntries) * * \param fp A pointer to an open FILE object */ -size_t NegCache::doDump(int fd, size_t maxCacheEntries) +size_t NegCache::doDump(int fd, size_t maxCacheEntries, time_t now) { int newfd = dup(fd); if (newfd == -1) { @@ -308,9 +308,6 @@ size_t NegCache::doDump(int fd, size_t maxCacheEntries) } fprintf(fp.get(), "; negcache dump follows\n;\n"); - struct timeval now; - Utility::gettimeofday(&now, nullptr); - size_t ret = 0; size_t shard = 0; @@ -326,7 +323,7 @@ size_t NegCache::doDump(int fd, size_t maxCacheEntries) auto& sidx = m->d_map.get(); for (const NegCacheEntry& ne : sidx) { ret++; - int64_t ttl = ne.d_ttd - now.tv_sec; + 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); 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()); diff --git a/pdns/recursordist/negcache.hh b/pdns/recursordist/negcache.hh index 0d42afee10..f8e8ce0be5 100644 --- a/pdns/recursordist/negcache.hh +++ b/pdns/recursordist/negcache.hh @@ -100,7 +100,7 @@ public: size_t count(const DNSName& qname, QType qtype); void prune(size_t maxEntries); void clear(); - size_t doDump(int fd, size_t maxCacheEntries); + size_t doDump(int fd, size_t maxCacheEntries, time_t now = time(nullptr)); size_t wipe(const DNSName& name, bool subtree = false); size_t wipeTyped(const DNSName& name, QType qtype); size_t size() const; diff --git a/pdns/recursordist/test-negcache_cc.cc b/pdns/recursordist/test-negcache_cc.cc index c432005ad0..96172d458f 100644 --- a/pdns/recursordist/test-negcache_cc.cc +++ b/pdns/recursordist/test-negcache_cc.cc @@ -526,7 +526,7 @@ BOOST_AUTO_TEST_CASE(test_dumpToFile) if (!fp) BOOST_FAIL("Temporary file could not be opened"); - cache.doDump(fileno(fp.get()), 0); + cache.doDump(fileno(fp.get()), 0, now.tv_sec); rewind(fp.get()); char* line = nullptr; -- 2.47.2