1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 05:01:29 +00:00

Avoid copying script results; store pointers instead.

This commit is contained in:
dmiller
2021-04-29 17:52:24 +00:00
parent c3a2faaa7b
commit f17fa67008
7 changed files with 38 additions and 34 deletions

View File

@@ -126,8 +126,7 @@ Target::~Target() {
#ifndef NOLUA #ifndef NOLUA
for (ScriptResults::iterator it = scriptResults.begin(); for (ScriptResults::iterator it = scriptResults.begin();
it != scriptResults.end(); it++) { it != scriptResults.end(); it++) {
ScriptResult sr = *it; delete (*it);
sr.clear();
} }
#endif #endif
} }

View File

@@ -2025,8 +2025,7 @@ int nmap_main(int argc, char *argv[]) {
printscriptresults(script_scan_results, SCRIPT_PRE_SCAN); printscriptresults(script_scan_results, SCRIPT_PRE_SCAN);
for (ScriptResults::iterator it = script_scan_results->begin(); for (ScriptResults::iterator it = script_scan_results->begin();
it != script_scan_results->end(); it++) { it != script_scan_results->end(); it++) {
ScriptResult sr = *it; delete (*it);
sr.clear();
} }
script_scan_results->clear(); script_scan_results->clear();
} }
@@ -2281,8 +2280,7 @@ int nmap_main(int argc, char *argv[]) {
printscriptresults(script_scan_results, SCRIPT_POST_SCAN); printscriptresults(script_scan_results, SCRIPT_POST_SCAN);
for (ScriptResults::iterator it = script_scan_results->begin(); for (ScriptResults::iterator it = script_scan_results->begin();
it != script_scan_results->end(); it++) { it != script_scan_results->end(); it++) {
ScriptResult sr = *it; delete (*it);
sr.clear();
} }
script_scan_results->clear(); script_scan_results->clear();
} }

View File

@@ -118,12 +118,12 @@ static int ports (lua_State *L)
static int script_set_output (lua_State *L) static int script_set_output (lua_State *L)
{ {
ScriptResult sr; ScriptResult *sr = new ScriptResult;
sr.set_id(luaL_checkstring(L, 1)); sr->set_id(luaL_checkstring(L, 1));
sr.set_output_tab(L, 2); sr->set_output_tab(L, 2);
if (!lua_isnil(L, 3)) { if (!lua_isnil(L, 3)) {
lua_len(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); script_scan_results.insert(sr);
return 0; return 0;
@@ -131,13 +131,13 @@ static int script_set_output (lua_State *L)
static int host_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); Target *target = nseU_gettarget(L, 1);
sr.set_id(luaL_checkstring(L, 2)); sr->set_id(luaL_checkstring(L, 2));
sr.set_output_tab(L, 3); sr->set_output_tab(L, 3);
if (!lua_isnil(L, 4)) { if (!lua_isnil(L, 4)) {
lua_len(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); target->scriptResults.insert(sr);
return 0; return 0;
@@ -147,14 +147,14 @@ static int port_set_output (lua_State *L)
{ {
Port *p; Port *p;
Port port; Port port;
ScriptResult sr; ScriptResult *sr = new ScriptResult;
Target *target = nseU_gettarget(L, 1); Target *target = nseU_gettarget(L, 1);
p = nseU_getport(L, target, &port, 2); p = nseU_getport(L, target, &port, 2);
sr.set_id(luaL_checkstring(L, 3)); sr->set_id(luaL_checkstring(L, 3));
sr.set_output_tab(L, 4); sr->set_output_tab(L, 4);
if (!lua_isnil(L, 5)) { if (!lua_isnil(L, 5)) {
lua_len(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.addScriptResult(p->portno, p->proto, sr);
target->ports.numscriptresults++; 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()); log_write(LOG_STDOUT, "ScriptResult::clear %d id %s\n", output_ref, get_id());
luaL_unref(L_NSE, LUA_REGISTRYINDEX, output_ref); luaL_unref(L_NSE, LUA_REGISTRYINDEX, output_ref);
output_ref = LUA_NOREF; output_ref = LUA_NOREF;
output_str.clear();
} }
void ScriptResult::set_output_tab (lua_State *L, int pos) 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); lua_pushvalue(L, pos);
output_ref = luaL_ref(L_NSE, LUA_REGISTRYINDEX); output_ref = luaL_ref(L_NSE, LUA_REGISTRYINDEX);
if (o.debugging > 3) if (o.debugging > 3)
@@ -465,11 +467,13 @@ std::string ScriptResult::get_output_str (void) const
return output_str; return output_str;
/* Auto-formatted table output? */ /* Auto-formatted table output? */
lua_rawgeti(L_NSE, LUA_REGISTRYINDEX, output_ref); if (output_ref != LUA_NOREF) {
if (!lua_isnil(L_NSE, -1)) lua_rawgeti(L_NSE, LUA_REGISTRYINDEX, output_ref);
output = format_obj(L_NSE, -1); if (!lua_isnil(L_NSE, -1))
output = format_obj(L_NSE, -1);
lua_pop(L_NSE, 1); lua_pop(L_NSE, 1);
}
return output; return output;
} }

View File

@@ -22,6 +22,10 @@ class ScriptResult
ScriptResult() { ScriptResult() {
output_ref = LUA_NOREF; output_ref = LUA_NOREF;
} }
~ScriptResult() {
// ensures Lua ref is released
clear();
}
void clear (void); void clear (void);
void set_output_tab (lua_State *, int); void set_output_tab (lua_State *, int);
void set_output_str (const char *); void set_output_str (const char *);
@@ -35,7 +39,7 @@ class ScriptResult
} }
}; };
typedef std::multiset<ScriptResult> ScriptResults; typedef std::multiset<ScriptResult *> ScriptResults;
/* Call this to get a ScriptResults object which can be /* Call this to get a ScriptResults object which can be
* used to store Pre-Scan and Post-Scan script Results */ * used to store Pre-Scan and Post-Scan script Results */

View File

@@ -446,7 +446,7 @@ std::string protect_xml(const std::string s) {
return r; return r;
} }
static char *formatScriptOutput(const ScriptResult &sr) { static char *formatScriptOutput(const ScriptResult *sr) {
std::vector<std::string> lines; std::vector<std::string> lines;
std::string c_output; std::string c_output;
@@ -454,7 +454,7 @@ static char *formatScriptOutput(const ScriptResult &sr) {
std::string result; std::string result;
unsigned int i; 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()) if (c_output.empty())
return NULL; return NULL;
p = c_output.c_str(); p = c_output.c_str();
@@ -478,7 +478,7 @@ static char *formatScriptOutput(const ScriptResult &sr) {
else else
result += "|_"; result += "|_";
if (i == 0) if (i == 0)
result += std::string(sr.get_id()) + ": "; result += std::string(sr->get_id()) + ": ";
result += lines[i]; result += lines[i];
if (i < lines.size() - 1) if (i < lines.size() - 1)
result += "\n"; result += "\n";
@@ -806,7 +806,7 @@ void printportoutput(const Target *currenths, const PortList *plist) {
ScriptResults::const_iterator ssr_iter; ScriptResults::const_iterator ssr_iter;
for (ssr_iter = current->scriptResults.begin(); for (ssr_iter = current->scriptResults.begin();
ssr_iter != current->scriptResults.end(); ssr_iter++) { ssr_iter != current->scriptResults.end(); ssr_iter++) {
ssr_iter->write_xml(); (*ssr_iter)->write_xml();
char *script_output = formatScriptOutput((*ssr_iter)); char *script_output = formatScriptOutput((*ssr_iter));
if (script_output != NULL) { if (script_output != NULL) {
@@ -2223,7 +2223,7 @@ void printscriptresults(const ScriptResults *scriptResults, stype scantype) {
log_write(LOG_PLAIN, "Post-scan script results:\n"); log_write(LOG_PLAIN, "Post-scan script results:\n");
} }
for (iter = scriptResults->begin(); iter != scriptResults->end(); iter++) { for (iter = scriptResults->begin(); iter != scriptResults->end(); iter++) {
iter->write_xml(); (*iter)->write_xml();
script_output = formatScriptOutput((*iter)); script_output = formatScriptOutput((*iter));
if (script_output != NULL) { if (script_output != NULL) {
log_write(LOG_PLAIN, "%s\n", script_output); log_write(LOG_PLAIN, "%s\n", script_output);
@@ -2244,7 +2244,7 @@ void printhostscriptresults(const Target *currenths) {
for (iter = currenths->scriptResults.begin(); for (iter = currenths->scriptResults.begin();
iter != currenths->scriptResults.end(); iter != currenths->scriptResults.end();
iter++) { iter++) {
iter->write_xml(); (*iter)->write_xml();
script_output = formatScriptOutput((*iter)); script_output = formatScriptOutput((*iter));
if (script_output != NULL) { if (script_output != NULL) {

View File

@@ -118,8 +118,7 @@ void Port::freeScriptResults(void)
#ifndef NOLUA #ifndef NOLUA
for (ScriptResults::iterator it = scriptResults.begin(); for (ScriptResults::iterator it = scriptResults.begin();
it != scriptResults.end(); it++) { it != scriptResults.end(); it++) {
ScriptResult sr = *it; delete (*it);
sr.clear();
} }
scriptResults.clear(); scriptResults.clear();
#endif #endif
@@ -374,7 +373,7 @@ void PortList::setServiceProbeResults(u16 portno, int protocol,
#ifndef NOLUA #ifndef NOLUA
void PortList::addScriptResult(u16 portno, int protocol, const ScriptResult& sr) { void PortList::addScriptResult(u16 portno, int protocol, ScriptResult *sr) {
Port *port; Port *port;
port = createPort(portno, protocol); port = createPort(portno, protocol);

View File

@@ -240,7 +240,7 @@ class PortList {
void getServiceDeductions(u16 portno, int protocol, struct serviceDeductions *sd) const; void getServiceDeductions(u16 portno, int protocol, struct serviceDeductions *sd) const;
#ifndef NOLUA #ifndef NOLUA
void addScriptResult(u16 portno, int protocol, const ScriptResult& sr); void addScriptResult(u16 portno, int protocol, ScriptResult *sr);
#endif #endif
/* Cycles through the 0 or more "ignored" ports which should be /* Cycles through the 0 or more "ignored" ports which should be