From 15ab34627ad1a68de68a20adedfedc1ebd826526 Mon Sep 17 00:00:00 2001 From: Daniel Miller Date: Thu, 2 Aug 2012 20:19:06 -0500 Subject: [PATCH 31/31] Wrap script-xml output in lua_pcall --- nse_main.cc | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/nse_main.cc b/nse_main.cc index ea21107..33fe427 100644 --- a/nse_main.cc +++ b/nse_main.cc @@ -514,19 +514,31 @@ ScriptResults *get_script_scan_results_obj (void) return &script_scan_results; } +static int script_output_xml (lua_State *L) +{ + if ( ! lua_isstring(L, -1) ) { + value_xml(L, -1, NSE_SR_DEPTH); + } + return 0; +} + void ScriptResult::write_xml() const { + int top; xml_open_start_tag("script"); xml_attribute("id", "%s", get_id()); xml_attribute("output", "%s", get_output()); xml_close_start_tag(); if (key != LUA_NOREF) { + top = lua_gettop(L_NSE); lua_getfield(L_NSE, LUA_REGISTRYINDEX, NSE_SCRIPT_RESULT); /* top+1 (results table)*/ - lua_rawgeti(L_NSE, -1, key); /* top+2 (output table) */ - if ( ! lua_isstring(L_NSE, -1) ) { - value_xml(L_NSE, -1, NSE_SR_DEPTH); - } - lua_pop(L_NSE, 2); + lua_pushcfunction(L_NSE, nseU_traceback); /* top+2 (msgh handler) */ + lua_pushcfunction(L_NSE, script_output_xml); /* top+3 (output function) */ + lua_rawgeti(L_NSE, -3, key); /* top+4 (output table) */ + if (lua_pcall(L_NSE, 1, 0, 1)) /* -2 + 1 */ + error("%s: Script Engine XML output aborted.\nAn error was thrown by the engine: %s", + SCRIPT_ENGINE, lua_tostring(L_NSE, -1)); + lua_settop(L_NSE, top); } xml_end_tag(); } -- 1.7.9.5