From 3dcc3fde593c689c8b413f52a9da77668e4e005b Mon Sep 17 00:00:00 2001 From: bert hubert Date: Wed, 18 May 2016 22:08:05 +0200 Subject: [PATCH] fix up memory leak on lua reload, thanks to "t4nk607" for spotting this issue. --- pdns/lua-recursor4.cc | 6 ++++-- pdns/lua-recursor4.hh | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pdns/lua-recursor4.cc b/pdns/lua-recursor4.cc index 4c04553bc..476326bca 100644 --- a/pdns/lua-recursor4.cc +++ b/pdns/lua-recursor4.cc @@ -7,8 +7,8 @@ #include "rec_channel.hh" #include "ednssubnet.hh" #include -#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(new LuaContext); d_lw->registerFunction("getID", [](dnsheader& dh) { return dh.id; }); d_lw->registerFunction("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(){} diff --git a/pdns/lua-recursor4.hh b/pdns/lua-recursor4.hh index 49353b055..31d9df58f 100644 --- a/pdns/lua-recursor4.hh +++ b/pdns/lua-recursor4.hh @@ -7,11 +7,15 @@ string GenUDPQueryResponse(const ComboAddress& dest, const string& query); class LuaContext; + class RecursorLua4 : public boost::noncopyable { +private: + std::unique_ptr 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& res, const vector >* ednsOpts, unsigned int tag, int& ret, bool* variable); bool nxdomain(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, int& ret, bool* variable); bool nodata(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, int& ret, bool* variable); @@ -57,7 +61,6 @@ private: DNSName followupName; }; - LuaContext* d_lw; typedef std::function)> 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& res, const vector >* ednsOpts, unsigned int tag, int& ret, bool* variable); -- 2.40.0