From: Russ Combs Date: Sat, 11 Oct 2014 11:29:46 +0000 (-0400) Subject: dabbled with exception for lua panic X-Git-Tag: 3.0.0-233~1388^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3106415875b4dc691ae49fa823d8caedb0aca3cf;p=thirdparty%2Fsnort3.git dabbled with exception for lua panic --- diff --git a/ChangeLog b/ChangeLog index cf57ab2ac..c1d43f6d2 100644 --- 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 diff --git a/src/main/shell.cc b/src/main/shell.cc index 97aeeeacd..f616b7612 100644 --- a/src/main/shell.cc +++ b/src/main/shell.cc @@ -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) { diff --git a/src/main/shell.h b/src/main/shell.h index a5a07a5e7..2b5cf2c24 100644 --- a/src/main/shell.h +++ b/src/main/shell.h @@ -22,6 +22,7 @@ #define SHELL_H #include +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; };