@@ -84,33 +84,39 @@ Port::Port() {
84
84
state_reason_init (&reason);
85
85
}
86
86
87
- void Port::freeService (bool del_service) {
88
- if (service != NULL ) {
89
- std::vector<char *>::iterator it;
90
-
91
- if (service->name )
92
- free (service->name );
93
- if (service->product )
94
- free (service->product );
95
- if (service->version )
96
- free (service->version );
97
- if (service->extrainfo )
98
- free (service->extrainfo );
99
- if (service->hostname )
100
- free (service->hostname );
101
- if (service->ostype )
102
- free (service->ostype );
103
- if (service->devicetype )
104
- free (service->devicetype );
105
- if (service->service_fp )
106
- free (service->service_fp );
107
- for (it = service->cpe .begin (); it != service->cpe .end (); it++)
108
- free (*it);
109
- service->cpe .clear ();
110
-
111
- if (del_service)
112
- delete service;
113
- }
87
+ void serviceDeductions::erase () {
88
+ std::vector<char *>::iterator it;
89
+
90
+ if (this ->product )
91
+ free (this ->product );
92
+ if (this ->version )
93
+ free (this ->version );
94
+ if (this ->extrainfo )
95
+ free (this ->extrainfo );
96
+ if (this ->hostname )
97
+ free (this ->hostname );
98
+ if (this ->ostype )
99
+ free (this ->ostype );
100
+ if (this ->devicetype )
101
+ free (this ->devicetype );
102
+ if (this ->service_fp )
103
+ free (this ->service_fp );
104
+ // For now, always free CPE strings
105
+ for (it = this ->cpe .begin (); it != this ->cpe .end (); it++)
106
+ free (*it);
107
+ this ->cpe .clear ();
108
+
109
+ this ->name = NULL ;
110
+ this ->name_confidence = 0 ;
111
+ this ->product = NULL ;
112
+ this ->version = NULL ;
113
+ this ->extrainfo = NULL ;
114
+ this ->hostname = NULL ;
115
+ this ->ostype = NULL ;
116
+ this ->devicetype = NULL ;
117
+ this ->service_tunnel = SERVICE_TUNNEL_NONE;
118
+ this ->service_fp = NULL ;
119
+ this ->dtype = SERVICE_DETECTION_TABLE;
114
120
}
115
121
116
122
void Port::freeScriptResults (void )
@@ -253,7 +259,6 @@ void serviceDeductions::populateFullVersionString(char *buf, size_t n) const {
253
259
// pass in an allocated struct serviceDeductions (don't worry about
254
260
// initializing, and you don't have to free any internal ptrs. See the
255
261
// serviceDeductions definition for the fields that are populated.
256
- // Returns 0 if at least a name is available.
257
262
void PortList::getServiceDeductions (u16 portno, int protocol, struct serviceDeductions *sd) const {
258
263
const Port *port;
259
264
@@ -265,7 +270,7 @@ void PortList::getServiceDeductions(u16 portno, int protocol, struct serviceDedu
265
270
*sd = serviceDeductions ();
266
271
service = nmap_getservbyport (portno, IPPROTO2STR (protocol));
267
272
if (service != NULL )
268
- sd->name = strdup ( service->s_name ) ;
273
+ sd->name = service->s_name ;
269
274
else
270
275
sd->name = NULL ;
271
276
sd->name_confidence = 3 ;
@@ -315,6 +320,8 @@ void PortList::setServiceProbeResults(u16 portno, int protocol,
315
320
port = createPort (portno, protocol);
316
321
if (port->service == NULL )
317
322
port->service = new serviceDeductions;
323
+ else
324
+ port->service ->erase ();
318
325
319
326
if (sres == PROBESTATE_FINISHED_HARDMATCHED
320
327
|| sres == PROBESTATE_FINISHED_SOFTMATCHED) {
@@ -341,10 +348,8 @@ void PortList::setServiceProbeResults(u16 portno, int protocol,
341
348
// port->serviceprobe_results = sres;
342
349
port->service ->service_tunnel = tunnel;
343
350
344
- port->freeService (false );
345
-
346
351
if (sname)
347
- port->service ->name = strdup ( sname) ;
352
+ port->service ->name = sname;
348
353
else
349
354
port->service ->name = NULL ;
350
355
@@ -426,10 +431,14 @@ PortList::~PortList() {
426
431
for (proto=0 ; proto < PORTLIST_PROTO_MAX; proto++) { // for every protocol
427
432
if (port_list[proto]) {
428
433
for (i=0 ; i < port_list_count[proto]; i++) { // free every Port
429
- if (port_list[proto][i]) {
430
- port_list[proto][i]->freeService (true );
431
- port_list[proto][i]->freeScriptResults ();
432
- delete port_list[proto][i];
434
+ Port *port = port_list[proto][i];
435
+ if (port) {
436
+ if (port->service ) {
437
+ port->service ->erase ();
438
+ delete port->service ;
439
+ }
440
+ port->freeScriptResults ();
441
+ delete port;
433
442
}
434
443
}
435
444
free (port_list[proto]);
0 commit comments