[PATCH:xdm] Stop leaking address struct on every call to getLocalAddress()

Alan Coopersmith alan.coopersmith at oracle.com
Mon Apr 1 23:44:42 PDT 2013


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 <ivansprundel at ioactive.com>
Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 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



More information about the xorg-devel mailing list