]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
dabbled with exception for lua panic
authorRuss Combs <rucombs@cisco.com>
Sat, 11 Oct 2014 11:29:46 +0000 (07:29 -0400)
committerRuss Combs <rucombs@cisco.com>
Sat, 11 Oct 2014 11:29:46 +0000 (07:29 -0400)
ChangeLog
src/main/shell.cc
src/main/shell.h

index cf57ab2accd87b8af5fb96271e44f2fbfebeda2d..c1d43f6d2e5cb8f43ae76a310bee0ef935167cef 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+125
+-- discovered can't catch exceptions thrown from Lua to C++; need to
+   build liblua differently
+
 124
 -- valgrind cleanup
 -- fixed incorrect casting of ftp, ftp-data, and telnet flow data
index 97aeeeacdcf03b81072e7c2a981db3b16b9ee1f6..f616b7612eb9b74d5ca56329bb65d6cc0ad9f77e 100644 (file)
@@ -38,7 +38,17 @@ static const char* required = "require('snort_config'); ";
 //-------------------------------------------------------------------------
 
 // FIXIT-L lua_pcall()s should be done safely to prevent panics from 
-// aborting process.
+// aborting process.  looks like need to compile lua into snort or build
+// it specially to ensure exceptions are caught through Lua.
+
+string Shell::fatal;
+
+int Shell::panic(lua_State* L)
+{
+    fatal = lua_tostring(L, -1);
+    throw runtime_error(fatal);
+    return -1;
+}
 
 // FIXIT-L --shell --pause should stop before loading config so Lua state
 // can be examined and modified.
@@ -121,6 +131,7 @@ static void config_lua(
 Shell::Shell(const char* s)
 {
     lua = luaL_newstate();
+    lua_atpanic(lua, Shell::panic);
     luaL_openlibs(lua);
 
     if ( s )
@@ -172,10 +183,19 @@ void Shell::install(const char* name, const luaL_Reg* reg)
 
 void Shell::execute(const char* cmd, string& rsp)
 {
-    int err = luaL_loadbuffer(lua, cmd, strlen(cmd), "shell");
+    int err = 0;
     
-    if ( !err )
-        err = lua_pcall(lua, 0, 0, 0);
+    try
+    {
+        err = luaL_loadbuffer(lua, cmd, strlen(cmd), "shell");
+    
+        if ( !err )
+            err = lua_pcall(lua, 0, 0, 0);
+    }
+    catch (...)
+    {
+        rsp = fatal.c_str();
+    }
 
     if (err)
     {
index a5a07a5e7f81c2f19a0f3c573bea3b6e309233ff..2b5cf2c24a9c87b83e41926ed1e1aa5baa4f728e 100644 (file)
@@ -22,6 +22,7 @@
 #define SHELL_H
 
 #include <string>
+struct lua_State;
 
 class Shell
 {
@@ -43,9 +44,13 @@ public:
     bool get_loaded() const
     { return loaded; };
 
+private:
+    static int panic(lua_State*);
+    static std::string fatal;
+
 private:
     bool loaded;
-    struct lua_State* lua;
+    lua_State* lua;
     std::string file;
     std::string overrides;
 };