Skip to content

Commit e4f6534

Browse files
committed
Use lua_createtable to prealloc some tables, saving time & memory
1 parent 7d724a6 commit e4f6534

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

nse_main.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static int ports (lua_State *L)
105105
while ((current = plist->nextPort(current, &port, TCPANDUDPANDSCTP,
106106
states[i])) != NULL)
107107
{
108-
lua_newtable(L);
108+
lua_createtable(L, 0, NSE_NUM_PORTINFO_FIELDS);
109109
set_portinfo(L, target, current);
110110
lua_pushboolean(L, 1);
111111
lua_rawset(L, -3);

nse_nmaplib.cc

+24-15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ extern NmapOps o;
3131
static const char *NSE_PROTOCOL_OP[] = {"tcp", "udp", "sctp", NULL};
3232
static const int NSE_PROTOCOL[] = {IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP};
3333

34+
// Number of fields in the "version" table
35+
// used as a hint to Lua when allocating it
36+
#define NSE_NUM_VERSION_FIELDS 12
37+
3438
void set_version (lua_State *L, const struct serviceDeductions *sd)
3539
{
3640
nseU_setsfield(L, -1, "name", sd->name);
@@ -50,7 +54,7 @@ void set_version (lua_State *L, const struct serviceDeductions *sd)
5054
sd->dtype == SERVICE_DETECTION_TABLE ? "table" :
5155
sd->dtype == SERVICE_DETECTION_PROBED ? "probed" :
5256
NULL);
53-
lua_newtable(L);
57+
lua_createtable(L, sd->cpe.size(), 0);
5458
for (size_t i = 0; i < sd->cpe.size(); i++) {
5559
lua_pushstring(L, sd->cpe[i]);
5660
lua_rawseti(L, -2, i+1);
@@ -73,7 +77,7 @@ void set_portinfo (lua_State *L, const Target *target, const Port *port)
7377
nseU_setsfield(L, -1, "state", statenum2str(port->state));
7478
nseU_setsfield(L, -1, "reason", reason_str(port->reason.reason_id, 1));
7579
nseU_setifield(L, -1, "reason_ttl", port->reason.ttl);
76-
lua_newtable(L);
80+
lua_createtable(L, 0, NSE_NUM_VERSION_FIELDS);
7781
set_version(L, &sd);
7882
lua_setfield(L, -2, "version");
7983
}
@@ -108,14 +112,15 @@ static void push_osclass_table(lua_State *L,
108112
const struct OS_Classification *osclass) {
109113
unsigned int i;
110114

111-
lua_newtable(L);
115+
#define NSE_NUM_OSCLASS_FIELDS 5
116+
lua_createtable(L, 0, NSE_NUM_OSCLASS_FIELDS);
112117

113118
set_string_or_nil(L, "vendor", osclass->OS_Vendor);
114119
set_string_or_nil(L, "osfamily", osclass->OS_Family);
115120
set_string_or_nil(L, "osgen", osclass->OS_Generation);
116121
set_string_or_nil(L, "type", osclass->Device_Type);
117122

118-
lua_newtable(L);
123+
lua_createtable(L, osclass->cpe.size(), 0);
119124
for (i = 0; i < osclass->cpe.size(); i++) {
120125
lua_pushstring(L, osclass->cpe[i]);
121126
lua_rawseti(L, -2, i + 1);
@@ -127,12 +132,13 @@ static void push_osmatch_table(lua_State *L, const FingerMatch *match,
127132
const OS_Classification_Results *OSR) {
128133
int i;
129134

130-
lua_newtable(L);
135+
#define NSE_NUM_OSMATCH_FIELDS 2
136+
lua_createtable(L, 0, NSE_NUM_OSMATCH_FIELDS);
131137

132138
lua_pushstring(L, match->OS_name);
133139
lua_setfield(L, -2, "name");
134140

135-
lua_newtable(L);
141+
lua_createtable(L, OSR->OSC_num_matches, 0);
136142
for (i = 0; i < OSR->OSC_num_matches; i++) {
137143
push_osclass_table(L, OSR->OSC[i]);
138144
lua_rawseti(L, -2, i + 1);
@@ -181,7 +187,8 @@ void set_hostinfo(lua_State *L, Target *currenths) {
181187
push_bin_ip(L, currenths->SourceSockAddr());
182188
lua_setfield(L, -2, "bin_ip_src");
183189

184-
lua_newtable(L);
190+
#define NSE_NUM_TIMES_FIELDS 3
191+
lua_createtable(L, 0, NSE_NUM_TIMES_FIELDS);
185192
nseU_setnfield(L, -1, "srtt", (lua_Number) currenths->to.srtt / 1000000.0);
186193
nseU_setnfield(L, -1, "rttvar", (lua_Number) currenths->to.rttvar / 1000000.0);
187194
nseU_setnfield(L, -1, "timeout", (lua_Number) currenths->to.timeout / 1000000.0);
@@ -195,10 +202,11 @@ void set_hostinfo(lua_State *L, Target *currenths) {
195202
{
196203
std::list<TracerouteHop>::iterator it;
197204

198-
lua_newtable(L);
205+
lua_createtable(L, currenths->traceroute_hops.size(), 0);
199206
for (it = currenths->traceroute_hops.begin(); it != currenths->traceroute_hops.end(); it++)
200207
{
201-
lua_newtable(L);
208+
#define NSE_NUM_TRACEROUTE_FIELDS 3
209+
lua_createtable(L, 0, NSE_NUM_TRACEROUTE_FIELDS);
202210
/* fill the table if the hop has not timed out, otherwise an empty table
203211
* is inserted */
204212
if (!it->timedout) {
@@ -230,7 +238,7 @@ void set_hostinfo(lua_State *L, Target *currenths) {
230238
int i;
231239
const OS_Classification_Results *OSR = FPR->getOSClassification();
232240

233-
lua_newtable(L);
241+
lua_createtable(L, FPR->num_perfect_matches, 0);
234242
for (i = 0; i < FPR->num_perfect_matches; i++) {
235243
push_osmatch_table(L, FPR->matches[i], OSR);
236244
lua_rawseti(L, -2, i + 1);
@@ -460,7 +468,7 @@ static int l_get_ports (lua_State *L)
460468
if (!(p = target->ports.nextPort(p, &port, protocol, state))) {
461469
lua_pushnil(L);
462470
} else {
463-
lua_newtable(L);
471+
lua_createtable(L, 0, NSE_NUM_PORTINFO_FIELDS);
464472
set_portinfo(L, target, p);
465473
}
466474
return 1;
@@ -487,7 +495,7 @@ static int l_get_port_state (lua_State *L)
487495
lua_pushnil(L);
488496
else
489497
{
490-
lua_newtable(L);
498+
lua_createtable(L, 0, NSE_NUM_PORTINFO_FIELDS);
491499
set_portinfo(L, target, p);
492500
}
493501
return 1;
@@ -791,7 +799,7 @@ static int l_get_dns_servers (lua_State *L)
791799
std::list<std::string> servs2 = get_dns_servers();
792800
std::list<std::string>::iterator servI2;
793801

794-
lua_newtable(L);
802+
lua_createtable(L, servs2.size(), 0);
795803
for (servI2 = servs2.begin(); servI2 != servs2.end(); servI2++)
796804
nseU_appendfstr(L, -1, "%s", servI2->c_str());
797805
return 1;
@@ -882,10 +890,11 @@ static int l_list_interfaces (lua_State *L)
882890
memset(ipstr, 0, INET6_ADDRSTRLEN);
883891
memset(&src, 0, sizeof(src));
884892
memset(&bcast, 0, sizeof(bcast));
885-
lua_newtable(L); //base table
893+
lua_createtable(L, numifs, 0); //base table
886894

887895
for(i=0; i< numifs; i++) {
888-
lua_newtable(L); //interface table
896+
#define NSE_NUM_INTERFACE_FIELDS 9
897+
lua_createtable(L, 0, NSE_NUM_INTERFACE_FIELDS); //interface table
889898
nseU_setsfield(L, -1, "device", iflist[i].devfullname);
890899
nseU_setsfield(L, -1, "shortname", iflist[i].devname);
891900
nseU_setifield(L, -1, "netmask", iflist[i].netmask_bits);

nse_nmaplib.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class Target;
77
class Port;
88

99
int luaopen_nmap(lua_State* l);
10+
#define NSE_NUM_HOSTINFO_FIELDS 17
1011
void set_hostinfo(lua_State* l, Target* currenths);
12+
#define NSE_NUM_PORTINFO_FIELDS 7
1113
void set_portinfo(lua_State* l, const Target *target, const Port* port);
1214

1315
#endif

nse_ssl_cert.cc

+15-10
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static void x509_name_to_table(lua_State *L, X509_NAME *name)
265265
{
266266
int i;
267267

268-
lua_newtable(L);
268+
lua_createtable(L, 0, X509_NAME_entry_count(name));
269269

270270
for (i = 0; i < X509_NAME_entry_count(name); i++) {
271271
X509_NAME_ENTRY *entry;
@@ -288,7 +288,7 @@ static bool x509_extensions_to_table(lua_State *L, const STACK_OF(X509_EXTENSION
288288
if (sk_X509_EXTENSION_num(exts) <= 0)
289289
return false;
290290

291-
lua_newtable(L);
291+
lua_createtable(L, sk_X509_EXTENSION_num(exts), 0);
292292

293293
for (int i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
294294
ASN1_OBJECT *obj;
@@ -299,7 +299,8 @@ static bool x509_extensions_to_table(lua_State *L, const STACK_OF(X509_EXTENSION
299299
ext = sk_X509_EXTENSION_value(exts, i);
300300
obj = X509_EXTENSION_get_object(ext);
301301

302-
lua_newtable(L);
302+
#define NSE_NUM_X509_EXTENSION_FIELDS 3
303+
lua_createtable(L, 0, NSE_NUM_X509_EXTENSION_FIELDS);
303304
char objname[256];
304305
long len = 0;
305306
len = OBJ_obj2txt(objname, 256, obj, 0);
@@ -324,7 +325,7 @@ static bool x509_extensions_to_table(lua_State *L, const STACK_OF(X509_EXTENSION
324325
}
325326
BIO_free_all(out);
326327

327-
lua_seti(L, -2, i+1);
328+
lua_rawseti(L, -2, i+1);
328329
}
329330

330331
return true;
@@ -418,7 +419,8 @@ static int time_to_tm(const ASN1_TIME *t, struct tm *result)
418419
that the wday and yday fields are not present. */
419420
static void tm_to_table(lua_State *L, const struct tm *tm)
420421
{
421-
lua_newtable(L);
422+
#define NSE_NUM_TM_FIELDS 6
423+
lua_createtable(L, 0, NSE_NUM_TM_FIELDS);
422424

423425
lua_pushnumber(L, tm->tm_year);
424426
lua_setfield(L, -2, "year");
@@ -459,7 +461,8 @@ static void asn1_time_to_obj(lua_State *L, const ASN1_TIME *s)
459461
from asn1_time_to_obj. */
460462
static void x509_validity_to_table(lua_State *L, X509 *cert)
461463
{
462-
lua_newtable(L);
464+
#define NSE_NUM_VALIDITY_FIELDS 2
465+
lua_createtable(L, 0, NSE_NUM_VALIDITY_FIELDS);
463466

464467
asn1_time_to_obj(L, X509_get0_notBefore(cert));
465468
lua_setfield(L, -2, "notBefore");
@@ -512,8 +515,8 @@ int lua_push_ecdhparams(lua_State *L, EVP_PKEY *pubkey) {
512515
const EC_GROUP *group = EC_KEY_get0_group(ec_key);
513516
int nid;
514517
/* This structure (ecdhparams.curve_params) comes from tls.lua */
515-
lua_newtable(L); /* ecdhparams */
516-
lua_newtable(L); /* curve_params */
518+
lua_createtable(L, 0, 1); /* ecdhparams */
519+
lua_createtable(L, 0, 2); /* curve_params */
517520
if ((nid = EC_GROUP_get_curve_name(group)) != 0) {
518521
lua_pushstring(L, OBJ_nid2sn(nid));
519522
lua_setfield(L, -2, "curve");
@@ -591,7 +594,8 @@ static int parse_ssl_cert(lua_State *L, X509 *cert)
591594
udata = (struct cert_userdata *) lua_newuserdata(L, sizeof(*udata));
592595
udata->cert = cert;
593596

594-
lua_newtable(L);
597+
#define NSE_NUM_CERT_FIELDS 7
598+
lua_createtable(L, 0, NSE_NUM_CERT_FIELDS);
595599

596600
subject = X509_get_subject_name(cert);
597601
if (subject != NULL) {
@@ -633,7 +637,8 @@ static int parse_ssl_cert(lua_State *L, X509 *cert)
633637
lua_pushfstring(L, "Error parsing cert: %s", ERR_error_string(ERR_get_error(), NULL));
634638
return 2;
635639
}
636-
lua_newtable(L);
640+
#define NSE_NUM_PKEY_FIELDS 4
641+
lua_createtable(L, 0, NSE_NUM_PKEY_FIELDS);
637642
#if HAVE_OPAQUE_STRUCTS
638643
pkey_type = EVP_PKEY_base_id(pubkey);
639644
#else

0 commit comments

Comments
 (0)