]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Handle calling PacketCache methods on a nil object 9356/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 28 Jul 2020 12:41:38 +0000 (14:41 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 28 Jul 2020 12:41:38 +0000 (14:41 +0200)
pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc

index b534651ac12627ad508026e9ce0568603b5b28cf..27c20b41b0b773ce27e88c5d52050184031c6abe 100644 (file)
@@ -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<std::string(std::shared_ptr<DNSDistPacketCache>::*)()>("toString", [](const std::shared_ptr<DNSDistPacketCache>& cache) {
+      if (cache) {
+        return cache->toString();
+      }
+      return std::string();
+    });
+  g_lua.registerFunction<bool(std::shared_ptr<DNSDistPacketCache>::*)()>("isFull", [](const std::shared_ptr<DNSDistPacketCache>& cache) {
+      if (cache) {
+        return cache->isFull();
+      }
+      return false;
+    });
+  g_lua.registerFunction<size_t(std::shared_ptr<DNSDistPacketCache>::*)(size_t)>("purgeExpired", [](std::shared_ptr<DNSDistPacketCache>& cache, size_t upTo) {
+      if (cache) {
+        return cache->purgeExpired(upTo);
+      }
+      return static_cast<size_t>(0);
+    });
+  g_lua.registerFunction<size_t(std::shared_ptr<DNSDistPacketCache>::*)(size_t)>("expunge", [](std::shared_ptr<DNSDistPacketCache>& cache, size_t upTo) {
+      if (cache) {
+        return cache->expunge(upTo);
+      }
+      return static_cast<size_t>(0);
+    });
   g_lua.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)(const DNSName& dname, boost::optional<uint16_t> qtype, boost::optional<bool> suffixMatch)>("expungeByName", [](
-              std::shared_ptr<DNSDistPacketCache> cache,
+              std::shared_ptr<DNSDistPacketCache>& cache,
               const DNSName& dname,
               boost::optional<uint16_t> qtype,
               boost::optional<bool> 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<void(std::shared_ptr<DNSDistPacketCache>::*)()>("printStats", [](const std::shared_ptr<DNSDistPacketCache> cache) {
+  g_lua.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)()>("printStats", [](const std::shared_ptr<DNSDistPacketCache>& 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::unordered_map<std::string, uint64_t>(std::shared_ptr<DNSDistPacketCache>::*)()>("getStats", [](const std::shared_ptr<DNSDistPacketCache> cache) {
+  g_lua.registerFunction<std::unordered_map<std::string, uint64_t>(std::shared_ptr<DNSDistPacketCache>::*)()>("getStats", [](const std::shared_ptr<DNSDistPacketCache>& cache) {
       std::unordered_map<std::string, uint64_t> stats;
       if (cache) {
         stats["entries"] = cache->getEntriesCount();
@@ -132,7 +152,7 @@ void setupLuaBindingsPacketCache()
       }
       return stats;
     });
-  g_lua.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)(const std::string& fname)>("dump", [](const std::shared_ptr<DNSDistPacketCache> cache, const std::string& fname) {
+  g_lua.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)(const std::string& fname)>("dump", [](const std::shared_ptr<DNSDistPacketCache>& cache, const std::string& fname) {
       if (cache) {
 
         int fd = open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);