[PATCH] Avoid leaks in res_init

Jakub Jelinek jakub@redhat.com
Wed Jul 21 17:39:00 GMT 2004


Hi!

#include <malloc.h>
#include <resolv.h>
#include <netdb.h>

int main (void)
{
  int i;
  mtrace ();
  for (i = 0; i < 50; ++i)
    {
      res_init ();
      gethostbyname ("www.google.com");
    }
}

leaks memory and if IPv6 is enabled also filehandles.
I have googled around but could not find a word about whether
res_init can or can't be called multiple times, but certainly
we need such an interface e.g. for nscd to reread resolv.conf
during nscd -i hosts.

2004-07-21  Jakub Jelinek  <jakub@redhat.com>

	* resolv/res_libc.c (res_init): If RES_INIT is set and
	_res.nscount > 0, call __res_nclose and free nsaddrs.

--- libc/resolv/res_libc.c.jj	2003-07-23 09:56:18.000000000 +0200
+++ libc/resolv/res_libc.c	2004-07-21 19:25:05.419402346 +0200
@@ -54,6 +54,14 @@ res_init(void) {
 		_res.retry = 4;
 	if (!(_res.options & RES_INIT))
 		_res.options = RES_DEFAULT;
+	else if (_res.nscount > 0) {
+		__res_nclose (&_res);	/* Close any VC sockets.  */
+
+		for (int ns = 0; ns < MAXNS; ns++) {
+			free (_res._u._ext.nsaddrs[ns]);
+			_res._u._ext.nsaddrs[ns] = NULL;
+		}
+	}
 
 	/*
 	 * This one used to initialize implicitly to zero, so unless the app

	Jakub



More information about the Libc-hacker mailing list