Skip to content

Commit

Permalink
Plug a memory leak.
Browse files Browse the repository at this point in the history
If ibv_get_device_list() returns null, there's obviously no device list
to free, so we can just return - and, as ibv_free_device_list() is not
guaranteed to do nothing when passed a null pointer and, in fact, will
try to dereference the null pointer, we *must* return without calling
ibv_free_device_list().

However, if it returns a list, but there are no devices in the list, we
still have to free the list - the list is terminated with a null
pointer, so it's not empty.

The loop for adding devices from the list will terminate immediately if
numdev is 0, so don't bother to check whether it's zero, just run the
loop, which will do nothing if numdev is 0, and fall through to free the
device list.

While we're at it, replace a goto with a break that does the same thing.
  • Loading branch information
guyharris committed Nov 27, 2019
1 parent dd50873 commit 3191c8b
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pcap-rdmasniff.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ rdmasniff_findalldevs(pcap_if_list_t *devlistp, char *err_str)
int ret = 0;

dev_list = ibv_get_device_list(&numdev);
if (!dev_list || !numdev) {
if (!dev_list) {
return 0;
}

Expand All @@ -426,11 +426,10 @@ rdmasniff_findalldevs(pcap_if_list_t *devlistp, char *err_str)
*/
if (!add_dev(devlistp, dev_list[i]->name, 0, "RDMA sniffer", err_str)) {
ret = -1;
goto out;
break;
}
}

out:
ibv_free_device_list(dev_list);
return ret;
}

0 comments on commit 3191c8b

Please sign in to comment.