rwrap: Free IPv6 name servers on close.
authorJakub Hrozek <jakub.hrozek@gmail.com>
Tue, 21 Oct 2014 13:00:36 +0000 (15:00 +0200)
committerAndreas Schneider <asn@samba.org>
Thu, 23 Oct 2014 12:13:13 +0000 (14:13 +0200)
rwrap's res_ninit allocates IPv6 name servers, but res_nclose didn't
free them, leading to a resource leak detectable with valgrind.

Signed-off-by: Jakub Hrozek <jakub.hrozek@gmail.com>
Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
src/resolv_wrapper.c
tests/test_res_init.c

index 8cc1e04..5334bd1 100644 (file)
@@ -125,6 +125,10 @@ static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
 }
 #endif /* NDEBUG RWRAP_LOG */
 
+#ifndef SAFE_FREE
+#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
+#endif
+
 #define NEXT_KEY(buf, key) do {                                        \
        (key) = (buf) ? strpbrk((buf), " \t") : NULL;           \
        if ((key) != NULL) {                                    \
@@ -1100,6 +1104,16 @@ int __res_init(void)
 
 static void rwrap_res_nclose(struct __res_state *state)
 {
+#ifdef HAVE_RESOLV_IPV6_NSADDRS
+       int i;
+
+       if (state != NULL) {
+               for (i = 0; i < state->_u._ext.nscount; i++) {
+                       SAFE_FREE(state->_u._ext.nsaddrs[i]);
+                       state->_u._ext.nssocks[i] = 0;
+               }
+       }
+#endif
        libc_res_nclose(state);
 }
 
index 0e2d58e..da39435 100644 (file)
@@ -171,6 +171,8 @@ static void test_res_ninit(void **state)
        inet_ntop(AF_INET6, &(sa6->sin6_addr), straddr, INET6_ADDRSTRLEN);
        assert_string_equal(nameservers[2], straddr);
 #endif
+
+       res_nclose(&dnsstate);
 }
 
 static void test_res_ninit_enoent(void **state)