diff --git a/Target.cc b/Target.cc index 6253a7a0f..f8ce8ea24 100644 --- a/Target.cc +++ b/Target.cc @@ -126,8 +126,7 @@ Target::~Target() { #ifndef NOLUA for (ScriptResults::iterator it = scriptResults.begin(); it != scriptResults.end(); it++) { - ScriptResult sr = *it; - sr.clear(); + delete (*it); } #endif } diff --git a/nmap.cc b/nmap.cc index 7a821d572..037803261 100644 --- a/nmap.cc +++ b/nmap.cc @@ -2025,8 +2025,7 @@ int nmap_main(int argc, char *argv[]) { printscriptresults(script_scan_results, SCRIPT_PRE_SCAN); for (ScriptResults::iterator it = script_scan_results->begin(); it != script_scan_results->end(); it++) { - ScriptResult sr = *it; - sr.clear(); + delete (*it); } script_scan_results->clear(); } @@ -2281,8 +2280,7 @@ int nmap_main(int argc, char *argv[]) { printscriptresults(script_scan_results, SCRIPT_POST_SCAN); for (ScriptResults::iterator it = script_scan_results->begin(); it != script_scan_results->end(); it++) { - ScriptResult sr = *it; - sr.clear(); + delete (*it); } script_scan_results->clear(); } diff --git a/nse_main.cc b/nse_main.cc index 4ffd44839..110291f57 100644 --- a/nse_main.cc +++ b/nse_main.cc @@ -118,12 +118,12 @@ static int ports (lua_State *L) static int script_set_output (lua_State *L) { - ScriptResult sr; - sr.set_id(luaL_checkstring(L, 1)); - sr.set_output_tab(L, 2); + ScriptResult *sr = new ScriptResult; + sr->set_id(luaL_checkstring(L, 1)); + sr->set_output_tab(L, 2); if (!lua_isnil(L, 3)) { lua_len(L, 3); - sr.set_output_str(luaL_checkstring(L, 3), luaL_checkinteger(L,-1)); + sr->set_output_str(luaL_checkstring(L, 3), luaL_checkinteger(L,-1)); } script_scan_results.insert(sr); return 0; @@ -131,13 +131,13 @@ static int script_set_output (lua_State *L) static int host_set_output (lua_State *L) { - ScriptResult sr; + ScriptResult *sr = new ScriptResult; Target *target = nseU_gettarget(L, 1); - sr.set_id(luaL_checkstring(L, 2)); - sr.set_output_tab(L, 3); + sr->set_id(luaL_checkstring(L, 2)); + sr->set_output_tab(L, 3); if (!lua_isnil(L, 4)) { lua_len(L, 4); - sr.set_output_str(luaL_checkstring(L, 4), luaL_checkinteger(L,-1)); + sr->set_output_str(luaL_checkstring(L, 4), luaL_checkinteger(L,-1)); } target->scriptResults.insert(sr); return 0; @@ -147,14 +147,14 @@ static int port_set_output (lua_State *L) { Port *p; Port port; - ScriptResult sr; + ScriptResult *sr = new ScriptResult; Target *target = nseU_gettarget(L, 1); p = nseU_getport(L, target, &port, 2); - sr.set_id(luaL_checkstring(L, 3)); - sr.set_output_tab(L, 4); + sr->set_id(luaL_checkstring(L, 3)); + sr->set_output_tab(L, 4); if (!lua_isnil(L, 5)) { lua_len(L, 5); - sr.set_output_str(luaL_checkstring(L, 5), luaL_checkinteger(L,-1)); + sr->set_output_str(luaL_checkstring(L, 5), luaL_checkinteger(L,-1)); } target->ports.addScriptResult(p->portno, p->proto, sr); target->ports.numscriptresults++; @@ -405,11 +405,13 @@ void ScriptResult::clear (void) log_write(LOG_STDOUT, "ScriptResult::clear %d id %s\n", output_ref, get_id()); luaL_unref(L_NSE, LUA_REGISTRYINDEX, output_ref); output_ref = LUA_NOREF; + output_str.clear(); } void ScriptResult::set_output_tab (lua_State *L, int pos) { - clear(); + // No reason to set output of a script twice unless you specifically cleared it. + assert(output_ref == LUA_NOREF); lua_pushvalue(L, pos); output_ref = luaL_ref(L_NSE, LUA_REGISTRYINDEX); if (o.debugging > 3) @@ -465,11 +467,13 @@ std::string ScriptResult::get_output_str (void) const return output_str; /* Auto-formatted table output? */ - lua_rawgeti(L_NSE, LUA_REGISTRYINDEX, output_ref); - if (!lua_isnil(L_NSE, -1)) - output = format_obj(L_NSE, -1); + if (output_ref != LUA_NOREF) { + lua_rawgeti(L_NSE, LUA_REGISTRYINDEX, output_ref); + if (!lua_isnil(L_NSE, -1)) + output = format_obj(L_NSE, -1); - lua_pop(L_NSE, 1); + lua_pop(L_NSE, 1); + } return output; } diff --git a/nse_main.h b/nse_main.h index 62e2e82d5..9bce74503 100644 --- a/nse_main.h +++ b/nse_main.h @@ -22,6 +22,10 @@ class ScriptResult ScriptResult() { output_ref = LUA_NOREF; } + ~ScriptResult() { + // ensures Lua ref is released + clear(); + } void clear (void); void set_output_tab (lua_State *, int); void set_output_str (const char *); @@ -35,7 +39,7 @@ class ScriptResult } }; -typedef std::multiset ScriptResults; +typedef std::multiset ScriptResults; /* Call this to get a ScriptResults object which can be * used to store Pre-Scan and Post-Scan script Results */ diff --git a/output.cc b/output.cc index 7a8f9e166..140ee30a5 100644 --- a/output.cc +++ b/output.cc @@ -446,7 +446,7 @@ std::string protect_xml(const std::string s) { return r; } -static char *formatScriptOutput(const ScriptResult &sr) { +static char *formatScriptOutput(const ScriptResult *sr) { std::vector lines; std::string c_output; @@ -454,7 +454,7 @@ static char *formatScriptOutput(const ScriptResult &sr) { std::string result; unsigned int i; - c_output = escape_for_screen(sr.get_output_str()); + c_output = escape_for_screen(sr->get_output_str()); if (c_output.empty()) return NULL; p = c_output.c_str(); @@ -478,7 +478,7 @@ static char *formatScriptOutput(const ScriptResult &sr) { else result += "|_"; if (i == 0) - result += std::string(sr.get_id()) + ": "; + result += std::string(sr->get_id()) + ": "; result += lines[i]; if (i < lines.size() - 1) result += "\n"; @@ -806,7 +806,7 @@ void printportoutput(const Target *currenths, const PortList *plist) { ScriptResults::const_iterator ssr_iter; for (ssr_iter = current->scriptResults.begin(); ssr_iter != current->scriptResults.end(); ssr_iter++) { - ssr_iter->write_xml(); + (*ssr_iter)->write_xml(); char *script_output = formatScriptOutput((*ssr_iter)); if (script_output != NULL) { @@ -2223,7 +2223,7 @@ void printscriptresults(const ScriptResults *scriptResults, stype scantype) { log_write(LOG_PLAIN, "Post-scan script results:\n"); } for (iter = scriptResults->begin(); iter != scriptResults->end(); iter++) { - iter->write_xml(); + (*iter)->write_xml(); script_output = formatScriptOutput((*iter)); if (script_output != NULL) { log_write(LOG_PLAIN, "%s\n", script_output); @@ -2244,7 +2244,7 @@ void printhostscriptresults(const Target *currenths) { for (iter = currenths->scriptResults.begin(); iter != currenths->scriptResults.end(); iter++) { - iter->write_xml(); + (*iter)->write_xml(); script_output = formatScriptOutput((*iter)); if (script_output != NULL) { diff --git a/portlist.cc b/portlist.cc index 110ef5883..6154c3b9a 100644 --- a/portlist.cc +++ b/portlist.cc @@ -118,8 +118,7 @@ void Port::freeScriptResults(void) #ifndef NOLUA for (ScriptResults::iterator it = scriptResults.begin(); it != scriptResults.end(); it++) { - ScriptResult sr = *it; - sr.clear(); + delete (*it); } scriptResults.clear(); #endif @@ -374,7 +373,7 @@ void PortList::setServiceProbeResults(u16 portno, int protocol, #ifndef NOLUA -void PortList::addScriptResult(u16 portno, int protocol, const ScriptResult& sr) { +void PortList::addScriptResult(u16 portno, int protocol, ScriptResult *sr) { Port *port; port = createPort(portno, protocol); diff --git a/portlist.h b/portlist.h index 640d477ca..9f31e635e 100644 --- a/portlist.h +++ b/portlist.h @@ -240,7 +240,7 @@ class PortList { void getServiceDeductions(u16 portno, int protocol, struct serviceDeductions *sd) const; #ifndef NOLUA - void addScriptResult(u16 portno, int protocol, const ScriptResult& sr); + void addScriptResult(u16 portno, int protocol, ScriptResult *sr); #endif /* Cycles through the 0 or more "ignored" ports which should be