2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-30 13:58:05 +00:00

Avoid malloc(0) and fix the loop invariant for the getifaddrs() case.

This commit is contained in:
Todd C. Miller
2003-03-14 02:17:38 +00:00
parent af7c4632f1
commit debf9f09c5

View File

@@ -121,7 +121,7 @@ load_interfaces()
return; return;
/* Allocate space for the interfaces list. */ /* Allocate space for the interfaces list. */
for (ifa = ifaddrs; ifa -> ifa_next; ifa = ifa -> ifa_next) { for (ifa = ifaddrs; ifa != NULL; ifa = ifa -> ifa_next) {
/* Skip interfaces marked "down" and "loopback". */ /* Skip interfaces marked "down" and "loopback". */
if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) || if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
(ifa->ifa_flags & IFF_LOOPBACK)) (ifa->ifa_flags & IFF_LOOPBACK))
@@ -134,11 +134,13 @@ load_interfaces()
break; break;
} }
} }
if (num_interfaces == 0)
return;
interfaces = interfaces =
(struct interface *) emalloc2(num_interfaces, sizeof(struct interface)); (struct interface *) emalloc2(num_interfaces, sizeof(struct interface));
/* Store the ip addr / netmask pairs. */ /* Store the ip addr / netmask pairs. */
for (ifa = ifaddrs, i = 0; ifa -> ifa_next; ifa = ifa -> ifa_next) { for (ifa = ifaddrs, i = 0; ifa != NULL; ifa = ifa -> ifa_next) {
/* Skip interfaces marked "down" and "loopback". */ /* Skip interfaces marked "down" and "loopback". */
if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) || if (ifa->ifa_addr == NULL || !(ifa->ifa_flags & IFF_UP) ||
(ifa->ifa_flags & IFF_LOOPBACK)) (ifa->ifa_flags & IFF_LOOPBACK))
@@ -191,7 +193,7 @@ load_interfaces()
} }
/* /*
* Get interface configuration or return (leaving num_interfaces 0) * Get interface configuration or return (leaving num_interfaces == 0)
*/ */
for (;;) { for (;;) {
ifconf_buf = erealloc(ifconf_buf, len); ifconf_buf = erealloc(ifconf_buf, len);
@@ -216,6 +218,8 @@ load_interfaces()
break; break;
len += BUFSIZ; len += BUFSIZ;
} }
if (n == 0)
return;
/* Allocate space for the maximum number of interfaces that could exist. */ /* Allocate space for the maximum number of interfaces that could exist. */
n = ifconf->ifc_len / sizeof(struct ifreq); n = ifconf->ifc_len / sizeof(struct ifreq);