diff options
author | 2013-03-20 14:15:56 +0000 | |
---|---|---|
committer | 2013-03-20 14:15:56 +0000 | |
commit | ebf4ec8f785c8c85b6fd6112b24b38ccc2b6cda1 (patch) | |
tree | 9fa497e174ee4872873aecfe4e13162806be1ea4 /lib/libc | |
parent | Move the __openbsd_randomdata_{start,end} symbols outside of the (diff) | |
download | wireguard-openbsd-ebf4ec8f785c8c85b6fd6112b24b38ccc2b6cda1.tar.xz wireguard-openbsd-ebf4ec8f785c8c85b6fd6112b24b38ccc2b6cda1.zip |
Use a realloc() loop around the sysctl() for NET_RT_IFLIST, in case an
interface is added at just the right... wrong moment.
ok millert dlg
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/net/getifaddrs.c | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/lib/libc/net/getifaddrs.c b/lib/libc/net/getifaddrs.c index 6f7ea157215..da42a23783e 100644 --- a/lib/libc/net/getifaddrs.c +++ b/lib/libc/net/getifaddrs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getifaddrs.c,v 1.10 2008/11/24 20:08:49 claudio Exp $ */ +/* $OpenBSD: getifaddrs.c,v 1.11 2013/03/20 14:15:56 deraadt Exp $ */ /* * Copyright (c) 1995, 1999 @@ -52,7 +52,7 @@ getifaddrs(struct ifaddrs **pif) int ncnt = 0; int mib[6]; size_t needed; - char *buf; + char *buf = NULL, *bufp; char *next; struct ifaddrs *cif = 0; char *p, *p0; @@ -74,13 +74,25 @@ getifaddrs(struct ifaddrs **pif) mib[3] = 0; /* wildcard address family */ mib[4] = NET_RT_IFLIST; mib[5] = 0; /* no flags */ - if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) - return (-1); - if ((buf = malloc(needed)) == NULL) - return (-1); - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { - free(buf); - return (-1); + while (1) { + if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) { + free(buf); + return (-1); + } + if (needed == 0) + break; + if ((bufp = realloc(buf, needed)) == NULL) { + free(buf); + return (-1); + } + buf = bufp; + if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { + if (errno == ENOMEM) + continue; + free(buf); + return (-1); + } + break; } for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { |