Nmap Development mailing list archives
Re: [nmap-svn] r32218 - nmap-exp/d33tah/ncat-sa-take2/ncat
From: Jacek Wielemborek <wielemborekj1 () gmail com>
Date: Fri, 6 Sep 2013 00:43:52 +0200
2013/9/6 <commit-mailer () nmap org>:
Author: david Date: Thu Sep 5 22:10:40 2013 New Revision: 32218 Log: Get rid of the global traceback index. Add a helper lua_call_traceback that pushes the traceback function before lua_pcall. The stack now has to be less meticulously maintained between lua_setup and lua_pcall. Modified: nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua.c nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua.h nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua_exec.c nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua_filters.c Modified: nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua.c ============================================================================== --- nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua.c (original) +++ nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua.c Thu Sep 5 22:10:40 2013 @@ -127,7 +127,6 @@ lua_State *filters_L = NULL; lua_State *luaexec_L = NULL; -int error_handler_idx = -1; void lua_report(lua_State *L, char *prefix, int panic) { @@ -177,13 +176,6 @@ *L = luaL_newstate(); luaL_openlibs(*L); - if (error_handler_idx == -1) { - /* install the traceback function */ - error_handler_idx = lua_gettop(*L); - lua_pushcfunction(*L, traceback); - lua_insert(*L, error_handler_idx); - } - if (script) lua_filters_setup(); } @@ -194,3 +186,14 @@ if (script) lua_run_filter(cmdexec); } + +/* Returns the value of a lua_pcall of the chunk on top of the stack, with an + error handler that prints a traceback. */ +int lua_call_traceback(lua_State *L, int nargs, int nresults) +{ + /* The chunk to run is on top of the stack. Put the traceback function + before it and run it. */ + lua_pushcfunction(L, traceback); + lua_insert(L, -2); + return lua_pcall(L, nargs, nresults, -2); +} Modified: nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua.h ============================================================================== --- nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua.h (original) +++ nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua.h Thu Sep 5 22:10:40 2013 @@ -139,10 +139,9 @@ extern lua_State *luaexec_L; extern lua_State *filters_L; -extern int error_handler_idx; - void lua_report(lua_State *L, char *prefix, int panic); void dump_stack(lua_State *L, char* title); void lua_setup(char *cmdexec, int script); +int lua_call_traceback(lua_State *L, int nargs, int nresults); #endif Modified: nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua_exec.c ============================================================================== --- nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua_exec.c (original) +++ nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua_exec.c Thu Sep 5 22:10:40 2013 @@ -126,9 +126,7 @@ void lua_run(void) { - if (lua_pcall(luaexec_L, 0, 0, error_handler_idx) != LUA_OK && !lua_isnil(luaexec_L, -1)) { - /* handle the error; the code below is taken from lua.c, Lua source code */ - lua_remove(luaexec_L, error_handler_idx); + if (lua_call_traceback(luaexec_L, 0, 0) != LUA_OK && !lua_isnil(luaexec_L, -1)) { lua_report(luaexec_L, "Error running the Lua script", 1); } else { if (o.debug) Modified: nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua_filters.c ============================================================================== --- nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua_filters.c (original) +++ nmap-exp/d33tah/ncat-sa-take2/ncat/ncat_lua_filters.c Thu Sep 5 22:10:40 2013 @@ -194,7 +194,7 @@ "end " "return ret " "end "); - lua_pcall(filters_L, 0, 1, error_handler_idx); + lua_call_traceback(filters_L, 0, 1); make_socket_function_idx = lua_gettop(filters_L); } @@ -221,7 +221,7 @@ void lua_run_filter(char *cmdexec) { - lua_pcall(filters_L, 0, 1, error_handler_idx); + lua_call_traceback(filters_L, 0, 1); if (!lua_istable(filters_L, -1)) bye("%s did not return a table.", cmdexec); /* Overwrite the socket variable with new_socket(socket_from_file, @@ -231,7 +231,7 @@ lua_pushvalue(filters_L, make_socket_function_idx); lua_insert(filters_L, -3); lua_fetch_registry("socket"); - if (lua_pcall(filters_L, 3, 1, error_handler_idx) != LUA_OK) + if (lua_call_traceback(filters_L, 3, 1) != LUA_OK) lua_report(filters_L, cmdexec, 1); lua_set_registry("socket"); } @@ -263,7 +263,7 @@ lua_pushvalue(filters_L, make_socket_function_idx); lua_pushvalue(filters_L, make_socket_function_idx); lua_fetch_registry("socket"); - if (lua_pcall(filters_L, 2, 1, error_handler_idx) != LUA_OK) + if (lua_call_traceback(filters_L, 2, 1) != LUA_OK) lua_report(filters_L, "Error creating the socket", 1); lua_pushvalue(filters_L, -1); /* Make a copy of the connection we created. */ lua_insert(filters_L, -4); /* Move it below connection, 5 and original table. */ _______________________________________________ Sent through the svn mailing list http://nmap.org/mailman/listinfo/svn
There's a problem with this helper - it doesn't work if nargs>1. That's because we move the argument to -2, which then would change the argument list... Also, keep in mind that we need to pop the traceback function. This is pretty complicated, because nret could be LUA_MULTRET, so we need to save the current stack top, compare it with the one after pcall, then calculate how many arguments we actually got (taking into account the nargs) and pop one value below the stack. Since I was actually working on something entirely somewhere else (more proper coroutines for recv()) and my stash rebased fine against your patches, I'll first finish my work against the tree before your changes and then get back to fixing this. _______________________________________________ Sent through the dev mailing list http://nmap.org/mailman/listinfo/dev Archived at http://seclists.org/nmap-dev/
Current thread:
- Re: [nmap-svn] r32218 - nmap-exp/d33tah/ncat-sa-take2/ncat Jacek Wielemborek (Sep 05)