From 21f7e4a8cdad2f29fda2768215c3a5352a9fa632 Mon Sep 17 00:00:00 2001 From: attilamolnar Date: Wed, 20 Mar 2013 23:43:51 +0100 Subject: Fix infinite loop when all DNS request slots are in use This is not the best way to detect this scenario, a better detection mechanism will replace this in the future --- src/dns.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src') diff --git a/src/dns.cpp b/src/dns.cpp index bede73a6f..63bde0ecc 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -258,8 +258,28 @@ DNSRequest* DNS::AddQuery(DNSHeader *header, int &id, const char* original) return NULL; /* Create an id */ + unsigned int tries = 0; do { id = ServerInstance->GenRandomInt(DNS::MAX_REQUEST_ID); + if (++tries == DNS::MAX_REQUEST_ID*5) + { + // If we couldn't find an empty slot this many times, do a sequential scan as a last + // resort. If an empty slot is found that way, go on, otherwise throw an exception + id = -1; + for (int i = 0; i < DNS::MAX_REQUEST_ID; i++) + { + if (!requests[i]) + { + id = i; + break; + } + } + + if (id == -1) + throw ModuleException("DNS: All ids are in use"); + + break; + } } while (requests[id]); DNSRequest* req = new DNSRequest(this, id, original); -- cgit v1.2.3