[Libwebsockets] getaddrinfo memory leak

Danomi Czaski djczaski at gmail.com
Fri Apr 10 19:59:21 CEST 2015


Valgrind complains of a small memory leak from getaddrinfo when
creating connections. The following patch seems to fix it:

diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index 40916d6..7169058 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -96,7 +96,7 @@ struct libwebsocket *libwebsocket_client_connect_2(
        } else
 #endif
        {
-               struct addrinfo ai, *res;
+               struct addrinfo ai, *res, *result;
                void *p = NULL;

                memset (&ai, 0, sizeof ai);
@@ -104,9 +104,10 @@ struct libwebsocket *libwebsocket_client_connect_2(
                ai.ai_socktype = SOCK_STREAM;
                ai.ai_flags = AI_CANONNAME;

-               if (getaddrinfo(ads, NULL, &ai, &res))
+               if (getaddrinfo(ads, NULL, &ai, &result))
                        goto oom4;

+               res = result;
                while (!p && res) {
                        switch (res->ai_family) {
                        case AF_INET:
@@ -118,11 +119,15 @@ struct libwebsocket *libwebsocket_client_connect_2(
                }

                if (!p)
+               {
+                       freeaddrinfo(result);
                        goto oom4;
+               }

                server_addr4.sin_family = AF_INET;
                server_addr4.sin_addr = *((struct in_addr *)p);
                bzero(&server_addr4.sin_zero, 8);
+               freeaddrinfo(result);
        }

        if (wsi->sock < 0) {



More information about the Libwebsockets mailing list