]> granicus.if.org Git - pdns/commitdiff
fix up memory leak on lua reload, thanks to "t4nk607" for spotting this issue.
authorbert hubert <bert.hubert@netherlabs.nl>
Wed, 18 May 2016 20:08:05 +0000 (22:08 +0200)
committerbert hubert <bert.hubert@netherlabs.nl>
Wed, 18 May 2016 20:08:05 +0000 (22:08 +0200)
pdns/lua-recursor4.cc
pdns/lua-recursor4.hh

index 4c04553bc3fb88edff2ffab4d530c60742017d65..476326bcab606e7815d87b7dd45c585e2afa9559 100644 (file)
@@ -7,8 +7,8 @@
 #include "rec_channel.hh" 
 #include "ednssubnet.hh"
 #include <unordered_set>
-#if !defined(HAVE_LUA)
 
+#if !defined(HAVE_LUA)
 RecursorLua4::RecursorLua4(const std::string &fname)
 {
   throw std::runtime_error("Attempt to load a Lua script in a PowerDNS binary without Lua support");
@@ -214,7 +214,7 @@ struct DynMetric
 
 RecursorLua4::RecursorLua4(const std::string& fname)
 {
-  d_lw = new LuaContext;
+  d_lw = std::unique_ptr<LuaContext>(new LuaContext);
 
   d_lw->registerFunction<int(dnsheader::*)()>("getID", [](dnsheader& dh) { return dh.id; });
   d_lw->registerFunction<bool(dnsheader::*)()>("getCD", [](dnsheader& dh) { return dh.cd; });
@@ -522,4 +522,6 @@ loop:;
   // see if they added followup work for us too
   return handled;
 }
+
 #endif
+RecursorLua4::~RecursorLua4(){}
index 49353b055bfa55bc5600a90e5bf7bc6dcd243a26..31d9df58f48e086b7fa48c592a21122f248079b6 100644 (file)
@@ -7,11 +7,15 @@
 string GenUDPQueryResponse(const ComboAddress& dest, const string& query);
 
 class LuaContext;
+
 class RecursorLua4 : public boost::noncopyable
 {
+private:
+  std::unique_ptr<LuaContext> d_lw; // this is way on top because it must get destroyed _last_
+
 public:
   explicit RecursorLua4(const std::string& fname);
-
+  ~RecursorLua4(); // this is so unique_ptr works with an incomplete type
   bool preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, const vector<pair<uint16_t,string> >* ednsOpts, unsigned int tag, int& ret, bool* variable);
   bool nxdomain(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, int& ret, bool* variable);
   bool nodata(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, int& ret, bool* variable);
@@ -57,7 +61,6 @@ private:
     DNSName followupName;
   };
 
-  LuaContext* d_lw;
   typedef std::function<bool(std::shared_ptr<DNSQuestion>)> luacall_t;
   luacall_t d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery;
   bool genhook(luacall_t& func, const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res,  const vector<pair<uint16_t,string> >* ednsOpts, unsigned int tag, int& ret, bool* variable);