getLocalAddress() checks the haveLocalAddress static variable to see
if it's already set up an address struct, and if not allocates memory
to do so and stores a pointer in the localAddress static variable.

Unfortunately, it never then set haveLocalAddress, so every time it was
called it simply overwrote the previous allocation with a new one.

Now we check to see if the allocation succeeded, and if so, then set
haveLocalAddress so we don't repeat and waste time & memory on later
calls.

Reported-by: Ilja Van Sprundel <[email protected]>
Signed-off-by: Alan Coopersmith <[email protected]>
---
 xdm/access.c |   54 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 32 insertions(+), 22 deletions(-)

diff --git a/xdm/access.c b/xdm/access.c
index b16a066..07b1e63 100644
--- a/xdm/access.c
+++ b/xdm/access.c
@@ -121,11 +121,10 @@ typedef struct _displayEntry {
 
 static DisplayEntry    *database;
 
-static ARRAY8          localAddress;
-
 ARRAY8Ptr
 getLocalAddress (void)
 {
+    static ARRAY8   localAddress;
     static int haveLocalAddress;
 
     if (!haveLocalAddress)
@@ -134,22 +133,29 @@ getLocalAddress (void)
        struct addrinfo *ai;
 
        if (getaddrinfo(localHostname(), NULL, NULL, &ai) != 0) {
-           XdmcpAllocARRAY8 (&localAddress, 4);
-           localAddress.data[0] = 127;
-           localAddress.data[1] = 0;
-           localAddress.data[2] = 0;
-           localAddress.data[3] = 1;
+           if (XdmcpAllocARRAY8 (&localAddress, 4)) {
+               localAddress.data[0] = 127;
+               localAddress.data[1] = 0;
+               localAddress.data[2] = 0;
+               localAddress.data[3] = 1;
+               haveLocalAddress = 1;
+           }
        } else {
            if (ai->ai_addr->sa_family == AF_INET) {
-               XdmcpAllocARRAY8 (&localAddress, sizeof(struct in_addr));
-               memcpy(localAddress.data,
-                 &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
-                 sizeof(struct in_addr));
+               if (XdmcpAllocARRAY8 (&localAddress, sizeof(struct in_addr))) {
+                   memcpy(localAddress.data,
+                          &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
+                          sizeof(struct in_addr));
+                   haveLocalAddress = 1;
+               }
            } else if (ai->ai_addr->sa_family == AF_INET6) {
-               XdmcpAllocARRAY8 (&localAddress, sizeof(struct in6_addr));
-               memcpy(localAddress.data,
-                 &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
-                 sizeof(struct in6_addr));
+               if (XdmcpAllocARRAY8 (&localAddress, sizeof(struct in6_addr)))
+               {
+                   memcpy(localAddress.data,
+                          &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
+                          sizeof(struct in6_addr));
+                   haveLocalAddress = 1;
+               }
            }
            freeaddrinfo(ai);
        }
@@ -158,15 +164,19 @@ getLocalAddress (void)
 
        hostent = gethostbyname (localHostname());
        if (hostent != NULL) {
-           XdmcpAllocARRAY8 (&localAddress, hostent->h_length);
-           memmove(localAddress.data, hostent->h_addr, hostent->h_length);
+           if (XdmcpAllocARRAY8 (&localAddress, hostent->h_length)) {
+               memmove(localAddress.data, hostent->h_addr, hostent->h_length);
+               haveLocalAddress = 1;
+           }
        } else {
            /* Assume 127.0.0.1 */
-           XdmcpAllocARRAY8 (&localAddress, 4);
-           localAddress.data[0] = 127;
-           localAddress.data[1] = 0;
-           localAddress.data[2] = 0;
-           localAddress.data[3] = 1;
+           if (XdmcpAllocARRAY8 (&localAddress, 4)) {
+               localAddress.data[0] = 127;
+               localAddress.data[1] = 0;
+               localAddress.data[2] = 0;
+               localAddress.data[3] = 1;
+               haveLocalAddress = 1;
+           }
        }
 # endif
 
-- 
1.7.9.2

_______________________________________________
[email protected]: X.Org development
Archives: http://lists.x.org/archives/xorg-devel
Info: http://lists.x.org/mailman/listinfo/xorg-devel

Reply via email to