From 2dcc0e58a054b7a0e8be90fd6524ff1e958d1ee7 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 28 Jul 2020 14:41:38 +0200 Subject: [PATCH] dnsdist: Handle calling PacketCache methods on a nil object --- .../dnsdist-lua-bindings-packetcache.cc | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc index b534651ac1..27c20b41b0 100644 --- a/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc @@ -92,12 +92,32 @@ void setupLuaBindingsPacketCache() return res; }); - g_lua.registerFunction("toString", &DNSDistPacketCache::toString); - g_lua.registerFunction("isFull", &DNSDistPacketCache::isFull); - g_lua.registerFunction("purgeExpired", &DNSDistPacketCache::purgeExpired); - g_lua.registerFunction("expunge", &DNSDistPacketCache::expunge); + g_lua.registerFunction::*)()>("toString", [](const std::shared_ptr& cache) { + if (cache) { + return cache->toString(); + } + return std::string(); + }); + g_lua.registerFunction::*)()>("isFull", [](const std::shared_ptr& cache) { + if (cache) { + return cache->isFull(); + } + return false; + }); + g_lua.registerFunction::*)(size_t)>("purgeExpired", [](std::shared_ptr& cache, size_t upTo) { + if (cache) { + return cache->purgeExpired(upTo); + } + return static_cast(0); + }); + g_lua.registerFunction::*)(size_t)>("expunge", [](std::shared_ptr& cache, size_t upTo) { + if (cache) { + return cache->expunge(upTo); + } + return static_cast(0); + }); g_lua.registerFunction::*)(const DNSName& dname, boost::optional qtype, boost::optional suffixMatch)>("expungeByName", []( - std::shared_ptr cache, + std::shared_ptr& cache, const DNSName& dname, boost::optional qtype, boost::optional suffixMatch) { @@ -105,7 +125,7 @@ void setupLuaBindingsPacketCache() g_outputBuffer="Expunged " + std::to_string(cache->expungeByName(dname, qtype ? *qtype : QType(QType::ANY).getCode(), suffixMatch ? *suffixMatch : false)) + " records\n"; } }); - g_lua.registerFunction::*)()>("printStats", [](const std::shared_ptr cache) { + g_lua.registerFunction::*)()>("printStats", [](const std::shared_ptr& cache) { if (cache) { g_outputBuffer="Entries: " + std::to_string(cache->getEntriesCount()) + "/" + std::to_string(cache->getMaxEntries()) + "\n"; g_outputBuffer+="Hits: " + std::to_string(cache->getHits()) + "\n"; @@ -117,7 +137,7 @@ void setupLuaBindingsPacketCache() g_outputBuffer+="TTL Too Shorts: " + std::to_string(cache->getTTLTooShorts()) + "\n"; } }); - g_lua.registerFunction(std::shared_ptr::*)()>("getStats", [](const std::shared_ptr cache) { + g_lua.registerFunction(std::shared_ptr::*)()>("getStats", [](const std::shared_ptr& cache) { std::unordered_map stats; if (cache) { stats["entries"] = cache->getEntriesCount(); @@ -132,7 +152,7 @@ void setupLuaBindingsPacketCache() } return stats; }); - g_lua.registerFunction::*)(const std::string& fname)>("dump", [](const std::shared_ptr cache, const std::string& fname) { + g_lua.registerFunction::*)(const std::string& fname)>("dump", [](const std::shared_ptr& cache, const std::string& fname) { if (cache) { int fd = open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660); -- 2.47.2