changeset 1219:f2d548ecdbde

Be more careful with allocated space, etc. in getifaddrs()
author gthomas
date Tue, 16 Sep 2003 22:05:05 +0000
parents e0c748e0a46b
children c87d732b5f4a
files packages/net/common/current/ChangeLog packages/net/common/current/src/ifaddrs.c
diffstat 2 files changed, 18 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/packages/net/common/current/ChangeLog
+++ b/packages/net/common/current/ChangeLog
@@ -1,3 +1,8 @@
+2003-09-16  Jay Foster  <jay@systech.com>
+ 
+ 	* src/ifaddrs.c (getifaddrs): Fix up allocation and freeing of
+    work buffers and data buffers.
+ 
 2003-07-29  Motoya Kurotsu  <kurotsu@allied-telesis.co.jp>
 
         * src/dhcp_prot.c (do_dhcp): (re)Initialize the lease when
--- a/packages/net/common/current/src/ifaddrs.c
+++ b/packages/net/common/current/src/ifaddrs.c
@@ -104,7 +104,8 @@ getifaddrs(struct ifaddrs **pif)
     int icnt = 1;  // Interface count
     int dcnt = 0;  // Data [length] count
     int ncnt = 0;  // Length of interface names
-    char buf[1024];
+    char *buf;
+#define	IF_WORK_SPACE_SZ	1024
     int i, sock;
 #ifdef CYGPKG_NET_INET6
     int sock6;
@@ -116,15 +117,22 @@ getifaddrs(struct ifaddrs **pif)
     char *data, *names;
     struct ifaddrs *ifa, *ift;
 
+    buf = malloc(IF_WORK_SPACE_SZ);
+    if (buf == NULL)
+        return (-1);
     ifc.ifc_buf = buf;
-    ifc.ifc_len = sizeof(buf);
+    ifc.ifc_len = IF_WORK_SPACE_SZ;
 
     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+    {
+        free(buf);
         return (-1);
+    }
     i =  ioctl(sock, SIOCGIFCONF, (char *)&ifc);
 
     if (i < 0) {
         close(sock); 
+        free(buf);
         return (-1);
     }
 
@@ -167,10 +175,11 @@ getifaddrs(struct ifaddrs **pif)
     ift = ifa;
 
     ifr = ifc.ifc_req;
-    lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
 
 #ifdef CYGPKG_NET_INET6
     if ((sock6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
+      free(buf);
+      free(data);
       close(sock);
       return (-1);
     }
@@ -234,6 +243,7 @@ getifaddrs(struct ifaddrs **pif)
             ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
         ift = (ift->ifa_next = ift + 1);
     }
+    free(buf);
 
     if (--ift >= ifa) {
         ift->ifa_next = NULL;