libc: Shortcut if_indextoname() if index == 0

If the index we're trying to convert is 0 we can avoid a potentially
expensive call to getifaddrs(). No interface has an ifindex of zero, so
we can handle this as an error: set the errno to ENXIO and return NULL.

Submitted by:	Nick Rogers
Reviewed by:	lutz at donnerhacke.de
MFC after:	2 weeks
Sponsored by:	RG Nets
Differential Revision:	https://reviews.freebsd.org/D24524
This commit is contained in:
Kristof Provost 2020-04-23 21:16:51 +00:00
parent 9d433cb875
commit 36dcd97de3

View file

@ -66,6 +66,11 @@ if_indextoname(unsigned int ifindex, char *ifname)
struct ifaddrs *ifaddrs, *ifa;
int error = 0;
if (ifindex == 0) {
errno = ENXIO;
return(NULL);
}
if (getifaddrs(&ifaddrs) < 0)
return(NULL); /* getifaddrs properly set errno */