summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2013-04-01 23:37:45 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2013-04-01 23:37:45 -0700
commitb1af740f2824b00fd2e073f847e0873b406bc1ad (patch)
tree85069764296dd92faf4a4d8236415ef9230f35ba
parent87f9c9d8cae9461f22f9b55056433336bea354c8 (diff)
Stop leaking address struct on every call to getLocalAddress()
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@ioactive.com> Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--xdm/access.c54
1 files 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