OSDN Git Service

fix if_nametoindex return value when interface does not exist
authorRich Felker <dalias@aerifal.cx>
Tue, 3 Jun 2014 21:53:11 +0000 (17:53 -0400)
committerRich Felker <dalias@aerifal.cx>
Tue, 3 Jun 2014 21:53:11 +0000 (17:53 -0400)
the return value is unsigned, so negative results for "errors" do not
make sense; 0 is the value reserved for when the interface name does
not exist.

src/network/if_nametoindex.c

index fb4a147..cb6ec05 100644 (file)
@@ -14,5 +14,5 @@ unsigned if_nametoindex(const char *name)
        strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
        r = ioctl(fd, SIOCGIFINDEX, &ifr);
        __syscall(SYS_close, fd);
-       return r < 0 ? r : ifr.ifr_ifindex;
+       return r < 0 ? 0 : ifr.ifr_ifindex;
 }