From af9f7f64eef86dfe7d438ff418969c86a37c5fa7 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Wed, 3 Mar 2021 10:33:12 +0100 Subject: [PATCH] dnsdist: Prevent an infinite loop when a cache shard is below the limit --- pdns/dnsdist-cache.cc | 3 +-- pdns/test-dnsdistpacketcache_cc.cc | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pdns/dnsdist-cache.cc b/pdns/dnsdist-cache.cc index 8bb38c2ff7..a5a3865662 100644 --- a/pdns/dnsdist-cache.cc +++ b/pdns/dnsdist-cache.cc @@ -305,6 +305,7 @@ size_t DNSDistPacketCache::purgeExpired(size_t upTo, const time_t now) do { uint32_t shardIndex = (d_expungeIndex++ % d_shardCount); + scannedMaps++; WriteLock w(&d_shards.at(shardIndex).d_lock); auto& map = d_shards.at(shardIndex).d_map; @@ -325,8 +326,6 @@ size_t DNSDistPacketCache::purgeExpired(size_t upTo, const time_t now) ++it; } } - - scannedMaps++; } while (scannedMaps < d_shardCount); diff --git a/pdns/test-dnsdistpacketcache_cc.cc b/pdns/test-dnsdistpacketcache_cc.cc index c39a304d11..3ac48628aa 100644 --- a/pdns/test-dnsdistpacketcache_cc.cc +++ b/pdns/test-dnsdistpacketcache_cc.cc @@ -112,6 +112,9 @@ BOOST_AUTO_TEST_CASE(test_PacketCacheSimple) { auto removed = PC.expungeByName(DNSName(" hello"), QType::ANY, true); BOOST_CHECK_EQUAL(PC.getSize(), 0U); BOOST_CHECK_EQUAL(removed, remaining); + + /* nothing to remove */ + BOOST_CHECK_EQUAL(PC.purgeExpired(0, now), 0U); } catch (const PDNSException& e) { cerr<<"Had error: "<