From 36dcd97de36daa64a387867e57fe48120256898a Mon Sep 17 00:00:00 2001 From: Kristof Provost Date: Thu, 23 Apr 2020 21:16:51 +0000 Subject: [PATCH] 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 --- lib/libc/net/if_indextoname.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/libc/net/if_indextoname.c b/lib/libc/net/if_indextoname.c index 908e771c4a4..454823e5e01 100644 --- a/lib/libc/net/if_indextoname.c +++ b/lib/libc/net/if_indextoname.c @@ -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 */