diff options
author | Guy Harris <guy@alum.mit.edu> | 2017-11-13 21:08:34 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2017-11-13 21:08:34 -0800 |
commit | c967f91f1f35f62bf0f07669e4d13f987451dc1b (patch) | |
tree | a665419819782248db4cb873d7b07bb6e39bdef5 /pcap-new.c | |
parent | 9e5dcd11abcd46b612dffa21cc4625b931b175f5 (diff) |
Plug memory leaks and clean up interface list creation.
If we fail in the middle of constructing lists of savefiles or remote
devices, free the entries we've already allocated.
Use separate variables for "current device" and "last device in the
list", to try to squelch some warnings.
We already rely on the pointer to the place where the device list
pointer should be put not being null; no need to handle the case where
it is null.
Diffstat (limited to 'pcap-new.c')
-rw-r--r-- | pcap-new.c | 48 |
1 files changed, 34 insertions, 14 deletions
@@ -72,9 +72,12 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all char name[PCAP_BUF_SIZE], path[PCAP_BUF_SIZE], filename[PCAP_BUF_SIZE]; pcap_t *fp; char tmpstring[PCAP_BUF_SIZE + 1]; /* Needed to convert names and descriptions from 'old' syntax to the 'new' one */ - pcap_if_t *dev; /* Previous device into the pcap_if_t chain */ + pcap_if_t *lastdev; /* Last device in the pcap_if_t list */ + pcap_if_t *dev; /* Device we're adding to the pcap_if_t list */ + /* List starts out empty. */ (*alldevs) = NULL; + lastdev = NULL; if (strlen(source) > PCAP_BUF_SIZE) { @@ -107,7 +110,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all if (pcap_findalldevs(alldevs, errbuf) == -1) return -1; - if ((alldevs == NULL) || (*alldevs == NULL)) + if (*alldevs == NULL) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "No interfaces found! Make sure libpcap/WinPcap is properly installed" @@ -132,6 +135,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all if (dev->name == NULL) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_freealldevs(*alldevs); return -1; } @@ -151,6 +155,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all if (dev->description == NULL) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_freealldevs(*alldevs); return -1; } @@ -224,6 +229,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all } #endif + /* Add all files we find to the list. */ do { @@ -238,30 +244,42 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all if (fp) { /* allocate the main structure */ - if (*alldevs == NULL) /* This is in case it is the first file */ - { - (*alldevs) = (pcap_if_t *)malloc(sizeof(pcap_if_t)); - dev = (*alldevs); - } - else - { - dev->next = (pcap_if_t *)malloc(sizeof(pcap_if_t)); - dev = dev->next; - } - - /* check that the malloc() didn't fail */ + dev = (pcap_if_t *)malloc(sizeof(pcap_if_t)); if (dev == NULL) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_freealldevs(*alldevs); return -1; } /* Initialize the structure to 'zero' */ memset(dev, 0, sizeof(pcap_if_t)); + /* Append it to the list. */ + if (lastdev == NULL) + { + /* + * List is empty, so it's also + * the first device. + */ + *alldevs = dev; + } + else + { + /* + * Append after the last device. + */ + lastdev->next = dev; + } + /* It's now the last device. */ + lastdev = dev; + /* Create the new source identifier */ if (pcap_createsrcstr(tmpstring, PCAP_SRC_FILE, NULL, NULL, filename, errbuf) == -1) + { + pcap_freealldevs(*alldevs); return -1; + } stringlen = strlen(tmpstring); @@ -269,6 +287,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all if (dev->name == NULL) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_freealldevs(*alldevs); return -1; } @@ -287,6 +306,7 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all if (dev->description == NULL) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_freealldevs(*alldevs); return -1; } |