]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Handle file-related errors when loading Lua scripts
authorFred Morcos <fred.morcos@open-xchange.com>
Wed, 3 Aug 2022 11:09:38 +0000 (13:09 +0200)
committerFred Morcos <fred.morcos@open-xchange.com>
Thu, 1 Sep 2022 08:31:52 +0000 (10:31 +0200)
pdns/lua-base4.cc
pdns/lua-base4.hh
pdns/recursordist/rec-main.cc

index a2da41bb75f5ecb20f482f2d7bb1be9aae915fb3..c82c93a047ad2be591e4ea64f6d180a235dab675 100644 (file)
@@ -1,3 +1,4 @@
+#include <cassert>
 #include <fstream>
 #include <unordered_set>
 #include <unordered_map>
 BaseLua4::BaseLua4() {
 }
 
-int BaseLua4::loadFile(const std::string& fname)
+void BaseLua4::loadFile(const std::string& fname)
 {
-  int ret = 0;
   std::ifstream ifs(fname);
   if (!ifs) {
-    ret = errno;
-    SLOG(g_log << Logger::Error << "Unable to read configuration file from '" << fname << "': " << stringerror(ret) << endl,
-         g_slog->withName("lua")->error(Logr::Error, ret, "Unable to read configuration file", "file", Logging::Loggable(fname)));
-    return ret;
+    auto ret = errno;
+    auto msg = stringerror(ret);
+    SLOG(g_log << Logger::Error << "Unable to read configuration file from '" << fname << "': " << msg << endl,
+         g_slog->withName("lua")->error(Logr::Error, ret, "Unable to read configuration file", "file", Logging::Loggable(fname), "msg", Logging::Loggable(msg)));
+    throw std::runtime_error(msg);
   }
   loadStream(ifs);
-  return 0;
 };
 
 void BaseLua4::loadString(const std::string &script) {
index 9394de4f8466c5390d8763f9a4e3b09a573956fc..1dbf62eecb39cca01a38d827ca7f5e5f3903c8b6 100644 (file)
@@ -13,7 +13,7 @@ protected:
 
 public:
   BaseLua4();
-  int loadFile(const std::string &fname);
+  void loadFile(const std::string &fname);
   void loadString(const std::string &script);
   void loadStream(std::istream &is);
   virtual ~BaseLua4(); // this is so unique_ptr works with an incomplete type
index 48685818dc42b52785f42efbee128414e26ce015..c248fcfc8b2d53bf8c1e26d474e359e0df2dcfdc 100644 (file)
@@ -2957,9 +2957,11 @@ static RecursorControlChannel::Answer* doReloadLuaScript()
     }
     else {
       t_pdl = std::make_shared<RecursorLua4>();
-      int err = t_pdl->loadFile(fname);
-      if (err != 0) {
-        string msg = std::to_string(RecThreadInfo::id()) + " Retaining current script, could not read '" + fname + "': " + stringerror(err);
+      try {
+        t_pdl->loadFile(fname);
+      }
+      catch (std::runtime_error& ex) {
+        string msg = std::to_string(RecThreadInfo::id()) + " Retaining current script, could not read '" + fname + "': " + ex.what();
         g_log << Logger::Error << msg << endl;
         return new RecursorControlChannel::Answer{1, msg + "\n"};
       }