From bf9d60734d4374e0d19212bc437adb22e4b0762d Mon Sep 17 00:00:00 2001 From: dmiller Date: Tue, 27 Apr 2021 19:22:09 +0000 Subject: [PATCH] Use std::multiset to keep script outputs in order instead of sorting. --- Target.cc | 7 ++++--- nmap.cc | 16 ++++++++++------ nse_main.cc | 4 ++-- nse_main.h | 7 +++++-- output.cc | 10 ---------- portlist.cc | 12 +++++++----- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Target.cc b/Target.cc index e7b84d2e7..01a2c1cf6 100644 --- a/Target.cc +++ b/Target.cc @@ -133,9 +133,10 @@ void Target::Recycle() { Target::~Target() { FreeInternal(); #ifndef NOLUA - while (!scriptResults.empty()) { - scriptResults.front().clear(); - scriptResults.pop_front(); + for (ScriptResults::iterator it = scriptResults.begin(); + it != scriptResults.end(); it++) { + ScriptResult sr = *it; + sr.clear(); } #endif } diff --git a/nmap.cc b/nmap.cc index 6e7750b71..40124103e 100644 --- a/nmap.cc +++ b/nmap.cc @@ -2026,10 +2026,12 @@ int nmap_main(int argc, char *argv[]) { script_scan_results = get_script_scan_results_obj(); script_scan(Targets, SCRIPT_PRE_SCAN); printscriptresults(script_scan_results, SCRIPT_PRE_SCAN); - while (!script_scan_results->empty()) { - script_scan_results->front().clear(); - script_scan_results->pop_front(); + for (ScriptResults::iterator it = script_scan_results->begin(); + it != script_scan_results->end(); it++) { + ScriptResult sr = *it; + sr.clear(); } + script_scan_results->clear(); } #endif @@ -2280,10 +2282,12 @@ int nmap_main(int argc, char *argv[]) { if (o.script) { script_scan(Targets, SCRIPT_POST_SCAN); printscriptresults(script_scan_results, SCRIPT_POST_SCAN); - while (!script_scan_results->empty()) { - script_scan_results->front().clear(); - script_scan_results->pop_front(); + for (ScriptResults::iterator it = script_scan_results->begin(); + it != script_scan_results->end(); it++) { + ScriptResult sr = *it; + sr.clear(); } + script_scan_results->clear(); delete new_targets; new_targets = NULL; } diff --git a/nse_main.cc b/nse_main.cc index b30ab48fd..4ffd44839 100644 --- a/nse_main.cc +++ b/nse_main.cc @@ -125,7 +125,7 @@ static int script_set_output (lua_State *L) lua_len(L, 3); sr.set_output_str(luaL_checkstring(L, 3), luaL_checkinteger(L,-1)); } - script_scan_results.push_back(sr); + script_scan_results.insert(sr); return 0; } @@ -139,7 +139,7 @@ static int host_set_output (lua_State *L) lua_len(L, 4); sr.set_output_str(luaL_checkstring(L, 4), luaL_checkinteger(L,-1)); } - target->scriptResults.push_back(sr); + target->scriptResults.insert(sr); return 0; } diff --git a/nse_main.h b/nse_main.h index d403f4634..62e2e82d5 100644 --- a/nse_main.h +++ b/nse_main.h @@ -2,7 +2,7 @@ #define NMAP_LUA_H #include -#include +#include #include #include "nse_lua.h" @@ -30,9 +30,12 @@ class ScriptResult void set_id (const char *); const char *get_id (void) const; void write_xml() const; + bool operator<(ScriptResult const &b) const { + return this->id.compare(b.id) < 0; + } }; -typedef std::list 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 edeb18436..d605235f1 100644 --- a/output.cc +++ b/output.cc @@ -446,12 +446,6 @@ std::string protect_xml(const std::string s) { return r; } -/* This is a helper function to determine the ordering of the script results - based on their id. */ -static bool scriptid_lessthan(const ScriptResult &a, const ScriptResult &b) { - return strcmp(a.get_id(), b.get_id()) < 0; -} - static char *formatScriptOutput(const ScriptResult &sr) { std::vector lines; @@ -810,8 +804,6 @@ void printportoutput(Target *currenths, PortList *plist) { #ifndef NOLUA if (o.script) { ScriptResults::const_iterator ssr_iter; - //Sort the results before outputting them on the screen - current->scriptResults.sort(scriptid_lessthan); for (ssr_iter = current->scriptResults.begin(); ssr_iter != current->scriptResults.end(); ssr_iter++) { ssr_iter->write_xml(); @@ -2223,7 +2215,6 @@ void printscriptresults(ScriptResults *scriptResults, stype scantype) { char *script_output; if (scriptResults->size() > 0) { - scriptResults->sort(scriptid_lessthan); if (scantype == SCRIPT_PRE_SCAN) { xml_start_tag("prescript"); log_write(LOG_PLAIN, "Pre-scan script results:\n"); @@ -2248,7 +2239,6 @@ void printhostscriptresults(Target *currenths) { char *script_output; if (currenths->scriptResults.size() > 0) { - currenths->scriptResults.sort(scriptid_lessthan); xml_start_tag("hostscript"); log_write(LOG_PLAIN, "\nHost script results:\n"); for (iter = currenths->scriptResults.begin(); diff --git a/portlist.cc b/portlist.cc index 12f701e1a..265d3bae3 100644 --- a/portlist.cc +++ b/portlist.cc @@ -116,10 +116,12 @@ void Port::freeService(bool del_service) { void Port::freeScriptResults(void) { #ifndef NOLUA - while (!scriptResults.empty()) { - scriptResults.front().clear(); - scriptResults.pop_front(); - } + for (ScriptResults::iterator it = scriptResults.begin(); + it != scriptResults.end(); it++) { + ScriptResult sr = *it; + sr.clear(); + } + scriptResults.clear(); #endif } @@ -377,7 +379,7 @@ void PortList::addScriptResult(u16 portno, int protocol, const ScriptResult& sr) port = createPort(portno, protocol); - port->scriptResults.push_back(sr); + port->scriptResults.insert(sr); } #endif