]> The Tcpdump Group git mirrors - libpcap/commitdiff
Only initialize np to NULL for Linux's getnetbyname_r().
authorGuy Harris <[email protected]>
Tue, 23 Oct 2018 08:35:56 +0000 (01:35 -0700)
committerGuy Harris <[email protected]>
Tue, 23 Oct 2018 08:35:56 +0000 (01:35 -0700)
That's the only case where it's set via a pointer rather than being set
to the return address of the function, so it's the only case where it
isn't guaranteed to be set (the man page nonwithstanding).

Put in a comment explaining *why* we're setting it.

nametoaddr.c

index e50b48215ab66214da5d1d43e5b138c7247aef11..5cfb51c4f06cec638a64ec5d6c340ee8f2ae4485 100644 (file)
@@ -239,7 +239,7 @@ pcap_nametonetaddr(const char *name)
        /*
         * UN*X.
         */
-       struct netent *np = NULL;
+       struct netent *np;
   #if defined(HAVE_LINUX_GETNETBYNAME_R)
        /*
         * We have Linux's reentrant getnetbyname_r().
@@ -249,7 +249,22 @@ pcap_nametonetaddr(const char *name)
        int h_errnoval;
        int err;
 
-       err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
+       /*
+        * Apparently, the man page at
+        *
+        *    http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
+        *
+        * lies when it says
+        *
+        *    If the function call successfully obtains a network record,
+        *    then *result is set pointing to result_buf; otherwise, *result
+        *    is set to NULL.
+        *
+        * and, in fact, at least in some versions of GNU libc, it does
+        * *not* always get set if getnetbyname_r() succeeds.
+        */
+       np = NULL;
+       err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
            &h_errnoval);
        if (err != 0) {
                /*