]> The Tcpdump Group git mirrors - libpcap/commitdiff
Don't drive on if fopen() or opendir() returns NULL.
authorGuy Harris <[email protected]>
Thu, 1 Dec 2011 06:45:03 +0000 (22:45 -0800)
committerGuy Harris <[email protected]>
Thu, 1 Dec 2011 06:45:03 +0000 (22:45 -0800)
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.

pcap-linux.c

index cedc2caf781b726c094b22bffd917cddcaf2f6ce..873a066e03bb1bc2e1e8e8411cde72c92fdbeb11 100644 (file)
@@ -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.