From: Guy Harris Date: Thu, 1 Dec 2011 06:45:03 +0000 (-0800) Subject: Don't drive on if fopen() or opendir() returns NULL. X-Git-Tag: libpcap-1.3-bp~28 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/bd93f801f1232508fcba3f8c2c5fe4f4372d3a49 Don't drive on if fopen() or opendir() returns NULL. Always either return 0, for "I can't scan that sysfs/procfs file because it doesn't exist", if the error is ENOENT, and return -1 with an error message for all other errors. Thanks to Michal Sekletar for finding that. --- diff --git a/pcap-linux.c b/pcap-linux.c index cedc2caf..873a066e 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -1890,8 +1890,20 @@ scan_sys_class_net(pcap_if_t **devlistp, char *errbuf) int ret = 1; sys_class_net_d = opendir("/sys/class/net"); - if (sys_class_net_d == NULL && errno == ENOENT) - return (0); + if (sys_class_net_d == NULL) { + /* + * Don't fail if it doesn't exist at all. + */ + if (errno == ENOENT) + return (0); + + /* + * Fail if we got some other error. + */ + (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, + "Can't open /sys/class/net: %s", pcap_strerror(errno)); + return (-1); + } /* * Create a socket from which to fetch interface information. @@ -2024,8 +2036,20 @@ scan_proc_net_dev(pcap_if_t **devlistp, char *errbuf) int ret = 0; proc_net_f = fopen("/proc/net/dev", "r"); - if (proc_net_f == NULL && errno == ENOENT) - return (0); + if (proc_net_f == NULL) { + /* + * Don't fail if it doesn't exist at all. + */ + if (errno == ENOENT) + return (0); + + /* + * Fail if we got some other error. + */ + (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, + "Can't open /proc/net/dev: %s", pcap_strerror(errno)); + return (-1); + } /* * Create a socket from which to fetch interface information.