Fix some memory leaks in arg parsing

This commit is contained in:
dmiller 2026-04-15 21:28:34 +00:00
parent 6ae2a33dc8
commit a35225d3e1
2 changed files with 15 additions and 8 deletions

View file

@ -1012,7 +1012,7 @@ int ArgParser::parseArguments(int argc, char *argv[]) {
if(strlen(optarg)==0)
nping_fatal(QT_3,"Invalid network interface supplied. Interface name cannot be NULL.");
else
o.setDevice( strdup(optarg) );
o.setDevice( optarg );
break; /* case 'e': */
case 'N': /* Don't capture packets */

View file

@ -2368,8 +2368,10 @@ if( this->getMode()!=TCP_CONNECT && this->getMode()!=UDP_UNPRIV && this->getRole
/* If that didn't work, ask libpcap */
if ( (dev = this->select_network_iface()) == NULL)
nping_fatal(QT_3, "Cannot obtain device for packet capture");
else
else {
this->setDevice( dev );
free(dev);
}
/* Libpcap gave us a device name, try to obtain it's IP */
if ( devname2ipaddr(this->getDevice(), this->af(), &ifaddr) != 0 ){
if( this->isRoot() )
@ -2390,8 +2392,10 @@ if( this->getMode()!=TCP_CONNECT && this->getMode()!=UDP_UNPRIV && this->getRole
char *selected_iface=this->select_network_iface();
if(selected_iface==NULL)
nping_fatal(QT_3, "Error trying to find a suitable network interface ");
else
else {
this->setDevice( selected_iface );
free(selected_iface);
}
}
} /* CASE 2: User did actually supply a device name */
else{
@ -2403,8 +2407,10 @@ if( this->getMode()!=TCP_CONNECT && this->getMode()!=UDP_UNPRIV && this->getRole
char *selected_iface=this->select_network_iface();
if(selected_iface==NULL)
nping_fatal(QT_3, "Error trying to find a suitable network interface ");
else
else {
this->setDevice( selected_iface );
free(selected_iface);
}
nping_print(DBG_2, "Using network interface \"%s\"", this->getDevice() );
}
@ -2755,6 +2761,7 @@ int NpingOps::cleanup(){
char *NpingOps::select_network_iface(){
char *devname = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *pcap_ifaces=NULL;
@ -2857,10 +2864,10 @@ char *NpingOps::select_network_iface(){
}
}
if(candidate==NULL)
return NULL;
else
return candidate->name;
if(candidate)
devname = strdup(candidate->name);
pcap_freealldevs(pcap_ifaces);
return devname;
} /* End of select_network_iface() */