From ffae2ddce64501226153261927178618d7d94a06 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Fri, 15 Apr 2022 12:46:44 +0200 Subject: [PATCH] dnsdist: Raise the number of entries in a packet cache to at least 1 And make sure that we cannot create such an object. --- pdns/dnsdist-cache.cc | 4 ++++ pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/pdns/dnsdist-cache.cc b/pdns/dnsdist-cache.cc index cdf02ad762..d6b99a0790 100644 --- a/pdns/dnsdist-cache.cc +++ b/pdns/dnsdist-cache.cc @@ -31,6 +31,10 @@ DNSDistPacketCache::DNSDistPacketCache(size_t maxEntries, uint32_t maxTTL, uint32_t minTTL, uint32_t tempFailureTTL, uint32_t maxNegativeTTL, uint32_t staleTTL, bool dontAge, uint32_t shards, bool deferrableInsertLock, bool parseECS): d_maxEntries(maxEntries), d_shardCount(shards), d_maxTTL(maxTTL), d_tempFailureTTL(tempFailureTTL), d_maxNegativeTTL(maxNegativeTTL), d_minTTL(minTTL), d_staleTTL(staleTTL), d_dontAge(dontAge), d_deferrableInsertLock(deferrableInsertLock), d_parseECS(parseECS) { + if (d_maxEntries == 0) { + throw std::runtime_error("Trying to create a 0-sized packet-cache"); + } + d_shards.resize(d_shardCount); /* we reserve maxEntries + 1 to avoid rehashing from occurring diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc index b77cd7f204..892de27f12 100644 --- a/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc @@ -101,6 +101,12 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client) } } + if (maxEntries == 0) { + warnlog("The number of entries in the packet cache is set to 0, raising to 1"); + g_outputBuffer += "The number of entries in the packet cache is set to 0, raising to 1"; + maxEntries = 1; + } + if (maxEntries < numberOfShards) { warnlog("The number of entries (%d) in the packet cache is smaller than the number of shards (%d), decreasing the number of shards to %d", maxEntries, numberOfShards, maxEntries); g_outputBuffer += "The number of entries (" + std::to_string(maxEntries) + " in the packet cache is smaller than the number of shards (" + std::to_string(numberOfShards) + "), decreasing the number of shards to " + std::to_string(maxEntries); -- 2.47.2