1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 13:11:28 +00:00

Use charpool for service names

Small strings have greater malloc alignment overhead which we save
(~300KB) with this method. string_pool was a slight memory savings but
greater computational cost due to memory overhead and insertion
calculation.
This commit is contained in:
dmiller
2022-09-19 17:51:46 +00:00
parent 00c493485c
commit e319c6d519
2 changed files with 7 additions and 18 deletions

View File

@@ -73,6 +73,7 @@
#include "nmap_error.h" #include "nmap_error.h"
#include "protocols.h" #include "protocols.h"
#include "scan_lists.h" #include "scan_lists.h"
#include "charpool.h"
#include "nmap_tty.h" #include "nmap_tty.h"
@@ -257,7 +258,6 @@ ServiceProbeMatch::ServiceProbeMatch() {
ServiceProbeMatch::~ServiceProbeMatch() { ServiceProbeMatch::~ServiceProbeMatch() {
std::vector<char *>::iterator it; std::vector<char *>::iterator it;
if (!isInitialized) return; if (!isInitialized) return;
if (servicename) free(servicename);
if (matchstr) free(matchstr); if (matchstr) free(matchstr);
if (product_template) free(product_template); if (product_template) free(product_template);
if (version_template) free(version_template); if (version_template) free(version_template);
@@ -375,9 +375,7 @@ void ServiceProbeMatch::InitMatch(const char *matchtext, int lineno) {
p = strchr(matchtext, ' '); p = strchr(matchtext, ' ');
if (!p) fatal("%s: parse error on line %d of nmap-service-probes: could not find service name", __func__, lineno); if (!p) fatal("%s: parse error on line %d of nmap-service-probes: could not find service name", __func__, lineno);
servicename = (char *) safe_malloc(p - matchtext + 1); servicename = cp_strndup(matchtext, p - matchtext);
memcpy(servicename, matchtext, p - matchtext);
servicename[p - matchtext] = '\0';
// The next part is a perl style regular expression specifier, like: // The next part is a perl style regular expression specifier, like:
// m/^220 .*smtp/i Where 'm' means a normal regular expressions is // m/^220 .*smtp/i Where 'm' means a normal regular expressions is
@@ -1085,9 +1083,6 @@ ServiceProbe::ServiceProbe() {
ServiceProbe::~ServiceProbe() { ServiceProbe::~ServiceProbe() {
std::vector<ServiceProbeMatch *>::iterator vi; std::vector<ServiceProbeMatch *>::iterator vi;
if (probename) free(probename);
if (probestring) free(probestring);
for(vi = matches.begin(); vi != matches.end(); vi++) { for(vi = matches.begin(); vi != matches.end(); vi++) {
delete *vi; delete *vi;
} }
@@ -1121,10 +1116,7 @@ void ServiceProbe::setProbeDetails(char *pd, int lineno) {
if (!isalnum((int) (unsigned char) *pd)) fatal("Parse error on line %d of nmap-service-probes - bad probe name", lineno); if (!isalnum((int) (unsigned char) *pd)) fatal("Parse error on line %d of nmap-service-probes - bad probe name", lineno);
p = strchr(pd, ' '); p = strchr(pd, ' ');
if (!p) fatal("Parse error on line %d of nmap-service-probes - nothing after probe name", lineno); if (!p) fatal("Parse error on line %d of nmap-service-probes - nothing after probe name", lineno);
len = p - pd; probename = cp_strndup(pd, p - pd);
probename = (char *) safe_malloc(len + 1);
memcpy(probename, pd, len);
probename[len] = '\0';
// Now for the probe itself // Now for the probe itself
pd = p+1; pd = p+1;
@@ -1141,12 +1133,9 @@ void ServiceProbe::setProbeDetails(char *pd, int lineno) {
} }
void ServiceProbe::setProbeString(const u8 *ps, int stringlen) { void ServiceProbe::setProbeString(const u8 *ps, int stringlen) {
if (probestringlen) free(probestring);
probestringlen = stringlen; probestringlen = stringlen;
if (stringlen > 0) { if (stringlen > 0) {
probestring = (u8 *) safe_malloc(stringlen + 1); probestring = (const u8 *)cp_strndup((const char *)ps, stringlen);
memcpy(probestring, ps, stringlen);
probestring[stringlen] = '\0'; // but note that other \0 may be in string
} else probestring = NULL; } else probestring = NULL;
} }

View File

@@ -155,7 +155,7 @@ class ServiceProbeMatch {
private: private:
int deflineno; // The line number where this match is defined. int deflineno; // The line number where this match is defined.
bool isInitialized; // Has InitMatch yet been called? bool isInitialized; // Has InitMatch yet been called?
char *servicename; const char *servicename;
char *matchstr; // Regular expression text char *matchstr; // Regular expression text
pcre *regex_compiled; pcre *regex_compiled;
pcre_extra *regex_extra; pcre_extra *regex_extra;
@@ -277,9 +277,9 @@ class ServiceProbe {
private: private:
void setPortVector(std::vector<u16> *portv, const char *portstr, void setPortVector(std::vector<u16> *portv, const char *portstr,
int lineno); int lineno);
char *probename; const char *probename;
u8 *probestring; const u8 *probestring;
int probestringlen; int probestringlen;
std::vector<u16> probableports; std::vector<u16> probableports;
std::vector<u16> probablesslports; std::vector<u16> probablesslports;