Skip to content

Commit bf9d607

Browse files
committed
Use std::multiset to keep script outputs in order instead of sorting.
1 parent 3adaa69 commit bf9d607

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

Target.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ void Target::Recycle() {
133133
Target::~Target() {
134134
FreeInternal();
135135
#ifndef NOLUA
136-
while (!scriptResults.empty()) {
137-
scriptResults.front().clear();
138-
scriptResults.pop_front();
136+
for (ScriptResults::iterator it = scriptResults.begin();
137+
it != scriptResults.end(); it++) {
138+
ScriptResult sr = *it;
139+
sr.clear();
139140
}
140141
#endif
141142
}

nmap.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -2026,10 +2026,12 @@ int nmap_main(int argc, char *argv[]) {
20262026
script_scan_results = get_script_scan_results_obj();
20272027
script_scan(Targets, SCRIPT_PRE_SCAN);
20282028
printscriptresults(script_scan_results, SCRIPT_PRE_SCAN);
2029-
while (!script_scan_results->empty()) {
2030-
script_scan_results->front().clear();
2031-
script_scan_results->pop_front();
2029+
for (ScriptResults::iterator it = script_scan_results->begin();
2030+
it != script_scan_results->end(); it++) {
2031+
ScriptResult sr = *it;
2032+
sr.clear();
20322033
}
2034+
script_scan_results->clear();
20332035
}
20342036
#endif
20352037

@@ -2280,10 +2282,12 @@ int nmap_main(int argc, char *argv[]) {
22802282
if (o.script) {
22812283
script_scan(Targets, SCRIPT_POST_SCAN);
22822284
printscriptresults(script_scan_results, SCRIPT_POST_SCAN);
2283-
while (!script_scan_results->empty()) {
2284-
script_scan_results->front().clear();
2285-
script_scan_results->pop_front();
2285+
for (ScriptResults::iterator it = script_scan_results->begin();
2286+
it != script_scan_results->end(); it++) {
2287+
ScriptResult sr = *it;
2288+
sr.clear();
22862289
}
2290+
script_scan_results->clear();
22872291
delete new_targets;
22882292
new_targets = NULL;
22892293
}

nse_main.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static int script_set_output (lua_State *L)
125125
lua_len(L, 3);
126126
sr.set_output_str(luaL_checkstring(L, 3), luaL_checkinteger(L,-1));
127127
}
128-
script_scan_results.push_back(sr);
128+
script_scan_results.insert(sr);
129129
return 0;
130130
}
131131

@@ -139,7 +139,7 @@ static int host_set_output (lua_State *L)
139139
lua_len(L, 4);
140140
sr.set_output_str(luaL_checkstring(L, 4), luaL_checkinteger(L,-1));
141141
}
142-
target->scriptResults.push_back(sr);
142+
target->scriptResults.insert(sr);
143143
return 0;
144144
}
145145

nse_main.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define NMAP_LUA_H
33

44
#include <vector>
5-
#include <list>
5+
#include <set>
66
#include <string>
77

88
#include "nse_lua.h"
@@ -30,9 +30,12 @@ class ScriptResult
3030
void set_id (const char *);
3131
const char *get_id (void) const;
3232
void write_xml() const;
33+
bool operator<(ScriptResult const &b) const {
34+
return this->id.compare(b.id) < 0;
35+
}
3336
};
3437

35-
typedef std::list<ScriptResult> ScriptResults;
38+
typedef std::multiset<ScriptResult> ScriptResults;
3639

3740
/* Call this to get a ScriptResults object which can be
3841
* used to store Pre-Scan and Post-Scan script Results */

output.cc

-10
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,6 @@ std::string protect_xml(const std::string s) {
446446
return r;
447447
}
448448

449-
/* This is a helper function to determine the ordering of the script results
450-
based on their id. */
451-
static bool scriptid_lessthan(const ScriptResult &a, const ScriptResult &b) {
452-
return strcmp(a.get_id(), b.get_id()) < 0;
453-
}
454-
455449
static char *formatScriptOutput(const ScriptResult &sr) {
456450
std::vector<std::string> lines;
457451

@@ -810,8 +804,6 @@ void printportoutput(Target *currenths, PortList *plist) {
810804
#ifndef NOLUA
811805
if (o.script) {
812806
ScriptResults::const_iterator ssr_iter;
813-
//Sort the results before outputting them on the screen
814-
current->scriptResults.sort(scriptid_lessthan);
815807
for (ssr_iter = current->scriptResults.begin();
816808
ssr_iter != current->scriptResults.end(); ssr_iter++) {
817809
ssr_iter->write_xml();
@@ -2223,7 +2215,6 @@ void printscriptresults(ScriptResults *scriptResults, stype scantype) {
22232215
char *script_output;
22242216

22252217
if (scriptResults->size() > 0) {
2226-
scriptResults->sort(scriptid_lessthan);
22272218
if (scantype == SCRIPT_PRE_SCAN) {
22282219
xml_start_tag("prescript");
22292220
log_write(LOG_PLAIN, "Pre-scan script results:\n");
@@ -2248,7 +2239,6 @@ void printhostscriptresults(Target *currenths) {
22482239
char *script_output;
22492240

22502241
if (currenths->scriptResults.size() > 0) {
2251-
currenths->scriptResults.sort(scriptid_lessthan);
22522242
xml_start_tag("hostscript");
22532243
log_write(LOG_PLAIN, "\nHost script results:\n");
22542244
for (iter = currenths->scriptResults.begin();

portlist.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ void Port::freeService(bool del_service) {
116116
void Port::freeScriptResults(void)
117117
{
118118
#ifndef NOLUA
119-
while (!scriptResults.empty()) {
120-
scriptResults.front().clear();
121-
scriptResults.pop_front();
122-
}
119+
for (ScriptResults::iterator it = scriptResults.begin();
120+
it != scriptResults.end(); it++) {
121+
ScriptResult sr = *it;
122+
sr.clear();
123+
}
124+
scriptResults.clear();
123125
#endif
124126
}
125127

@@ -377,7 +379,7 @@ void PortList::addScriptResult(u16 portno, int protocol, const ScriptResult& sr)
377379

378380
port = createPort(portno, protocol);
379381

380-
port->scriptResults.push_back(sr);
382+
port->scriptResults.insert(sr);
381383
}
382384
#endif
383385

0 commit comments

Comments
 (0)