Fix libnethogs handle memory leak

Calling nethogsmonitor_loop, canceling it with nethogsmonitor_breakloop
and then calling it again will lead to a Segmentation fault.

This is due to handles not getting reset to NULL which leads to a
invalid list since the old list will be reused by

  new handle(newhandle, current_dev->name, handles);

in nethogsmonitor_init(). If handles is reset to NULL it will also
remove the only reference to the old list, hence it should be freed to
avoid memory leaks.

This change will reset handles to NULL after freeing all handles when
nethogsmonitor_clean_up is called.
This commit is contained in:
d4ryus
2018-11-11 11:17:55 +01:00
parent 2e8a4f8b8b
commit 51ccf42cea

View File

@@ -259,10 +259,14 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) {
static void nethogsmonitor_clean_up() { static void nethogsmonitor_clean_up() {
// clean up // clean up
handle *current_handle = handles; handle *current_handle = handles;
handle *rem;
while (current_handle != NULL) { while (current_handle != NULL) {
pcap_close(current_handle->content->pcap_handle); pcap_close(current_handle->content->pcap_handle);
rem = current_handle;
current_handle = current_handle->next; current_handle = current_handle->next;
free(rem);
} }
handles = NULL;
// close file descriptors // close file descriptors
for (std::vector<int>::const_iterator it = pc_loop_fd_list.begin(); for (std::vector<int>::const_iterator it = pc_loop_fd_list.begin();