ce426f
Sourceware bug 16574
ce426f
ce426f
commit d668061994a7486a3ba9c7d5e7882d85a2883707
ce426f
Author: Andreas Schwab <schwab@suse.de>
ce426f
Date:   Thu Feb 13 11:01:57 2014 +0100
ce426f
ce426f
    Fix memory leak in _nss_dns_gethostbyname4_r with big DNS answer
ce426f
ce426f
commit ab7ac0f2cf8731fe4c3f3aea6088a7c0127b5725
ce426f
Author: Ondřej Bílka <neleai@seznam.cz>
ce426f
Date:   Sun Feb 16 12:59:23 2014 +0100
ce426f
ce426f
    Deduplicate resolv/nss_dns/dns-host.c
ce426f
    
ce426f
    In resolv/nss_dns/dns-host.c one of code path duplicated code after
ce426f
    that. We merge these paths.
ce426f
ce426f
commit ab09bf616ad527b249aca5f2a4956fd526f0712f
ce426f
Author: Andreas Schwab <schwab@suse.de>
ce426f
Date:   Tue Feb 18 10:57:25 2014 +0100
ce426f
ce426f
    Properly fix memory leak in _nss_dns_gethostbyname4_r with big DNS answer
ce426f
    
ce426f
    Instead of trying to guess whether the second buffer needs to be freed
ce426f
    set a flag at the place it is allocated
ce426f
ce426f
Index: glibc-2.17-c758a686/include/resolv.h
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/include/resolv.h
ce426f
+++ glibc-2.17-c758a686/include/resolv.h
ce426f
@@ -56,11 +56,11 @@ libc_hidden_proto (__res_randomid)
ce426f
 libc_hidden_proto (__res_state)
ce426f
 
ce426f
 int __libc_res_nquery (res_state, const char *, int, int, u_char *, int,
ce426f
-		       u_char **, u_char **, int *, int *);
ce426f
+		       u_char **, u_char **, int *, int *, int *);
ce426f
 int __libc_res_nsearch (res_state, const char *, int, int, u_char *, int,
ce426f
-			u_char **, u_char **, int *, int *);
ce426f
+			u_char **, u_char **, int *, int *, int *);
ce426f
 int __libc_res_nsend (res_state, const u_char *, int, const u_char *, int,
ce426f
-		      u_char *, int, u_char **, u_char **, int *, int *)
ce426f
+		      u_char *, int, u_char **, u_char **, int *, int *, int *)
ce426f
   attribute_hidden;
ce426f
 
ce426f
 libresolv_hidden_proto (_sethtent)
ce426f
Index: glibc-2.17-c758a686/resolv/gethnamaddr.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/resolv/gethnamaddr.c
ce426f
+++ glibc-2.17-c758a686/resolv/gethnamaddr.c
ce426f
@@ -616,7 +616,7 @@ gethostbyname2(name, af)
ce426f
 	buf.buf = origbuf = (querybuf *) alloca (1024);
ce426f
 
ce426f
 	if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf.buf->buf, 1024,
ce426f
-				    &buf.ptr, NULL, NULL, NULL)) < 0) {
ce426f
+				    &buf.ptr, NULL, NULL, NULL, NULL)) < 0) {
ce426f
 		if (buf.buf != origbuf)
ce426f
 			free (buf.buf);
ce426f
 		Dprintf("res_nsearch failed (%d)\n", n);
ce426f
@@ -711,12 +711,12 @@ gethostbyaddr(addr, len, af)
ce426f
 	buf.buf = orig_buf = (querybuf *) alloca (1024);
ce426f
 
ce426f
 	n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
ce426f
-			      &buf.ptr, NULL, NULL, NULL);
ce426f
+			      &buf.ptr, NULL, NULL, NULL, NULL);
ce426f
 	if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) {
ce426f
 		strcpy(qp, "ip6.int");
ce426f
 		n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf,
ce426f
 				      buf.buf != orig_buf ? MAXPACKET : 1024,
ce426f
-				      &buf.ptr, NULL, NULL, NULL);
ce426f
+				      &buf.ptr, NULL, NULL, NULL, NULL);
ce426f
 	}
ce426f
 	if (n < 0) {
ce426f
 		if (buf.buf != orig_buf)
ce426f
Index: glibc-2.17-c758a686/resolv/nss_dns/dns-canon.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/resolv/nss_dns/dns-canon.c
ce426f
+++ glibc-2.17-c758a686/resolv/nss_dns/dns-canon.c
ce426f
@@ -61,7 +61,7 @@ _nss_dns_getcanonname_r (const char *nam
ce426f
     {
ce426f
       int r = __libc_res_nquery (&_res, name, ns_c_in, qtypes[i],
ce426f
 				 buf, sizeof (buf), &ansp.ptr, NULL, NULL,
ce426f
-				 NULL);
ce426f
+				 NULL, NULL);
ce426f
       if (r > 0)
ce426f
 	{
ce426f
 	  /* We need to decode the response.  Just one question record.
ce426f
Index: glibc-2.17-c758a686/resolv/nss_dns/dns-host.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/resolv/nss_dns/dns-host.c
ce426f
+++ glibc-2.17-c758a686/resolv/nss_dns/dns-host.c
ce426f
@@ -190,7 +190,7 @@ _nss_dns_gethostbyname3_r (const char *n
ce426f
   host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
ce426f
 
ce426f
   n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf,
ce426f
-			  1024, &host_buffer.ptr, NULL, NULL, NULL);
ce426f
+			  1024, &host_buffer.ptr, NULL, NULL, NULL, NULL);
ce426f
   if (n < 0)
ce426f
     {
ce426f
       switch (errno)
ce426f
@@ -225,7 +225,7 @@ _nss_dns_gethostbyname3_r (const char *n
ce426f
 	n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf,
ce426f
 				host_buffer.buf != orig_host_buffer
ce426f
 				? MAXPACKET : 1024, &host_buffer.ptr,
ce426f
-				NULL, NULL, NULL);
ce426f
+				NULL, NULL, NULL, NULL);
ce426f
 
ce426f
       if (n < 0)
ce426f
 	{
ce426f
@@ -308,13 +308,20 @@ _nss_dns_gethostbyname4_r (const char *n
ce426f
   u_char *ans2p = NULL;
ce426f
   int nans2p = 0;
ce426f
   int resplen2 = 0;
ce426f
+  int ans2p_malloced = 0;
ce426f
 
ce426f
   int olderr = errno;
ce426f
   enum nss_status status;
ce426f
   int n = __libc_res_nsearch (&_res, name, C_IN, T_UNSPEC,
ce426f
 			      host_buffer.buf->buf, 2048, &host_buffer.ptr,
ce426f
-			      &ans2p, &nans2p, &resplen2);
ce426f
-  if (n < 0)
ce426f
+			      &ans2p, &nans2p, &resplen2, &ans2p_malloced);
ce426f
+  if (n >= 0)
ce426f
+    {
ce426f
+      status = gaih_getanswer (host_buffer.buf, n, (const querybuf *) ans2p,
ce426f
+			       resplen2, name, pat, buffer, buflen,
ce426f
+			       errnop, herrnop, ttlp);
ce426f
+    }
ce426f
+  else
ce426f
     {
ce426f
       switch (errno)
ce426f
 	{
ce426f
@@ -341,16 +348,11 @@ _nss_dns_gethostbyname4_r (const char *n
ce426f
 	*errnop = EAGAIN;
ce426f
       else
ce426f
 	__set_errno (olderr);
ce426f
-
ce426f
-      if (host_buffer.buf != orig_host_buffer)
ce426f
-	free (host_buffer.buf);
ce426f
-
ce426f
-      return status;
ce426f
     }
ce426f
 
ce426f
-  status = gaih_getanswer(host_buffer.buf, n, (const querybuf *) ans2p,
ce426f
-			  resplen2, name, pat, buffer, buflen,
ce426f
-			  errnop, herrnop, ttlp);
ce426f
+  /* Check whether ans2p was separately allocated.  */
ce426f
+  if (ans2p_malloced)
ce426f
+    free (ans2p);
ce426f
 
ce426f
   if (host_buffer.buf != orig_host_buffer)
ce426f
     free (host_buffer.buf);
ce426f
@@ -460,7 +462,7 @@ _nss_dns_gethostbyaddr2_r (const void *a
ce426f
 	  strcpy (qp, "].ip6.arpa");
ce426f
 	  n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR,
ce426f
 				 host_buffer.buf->buf, 1024, &host_buffer.ptr,
ce426f
-				 NULL, NULL, NULL);
ce426f
+				 NULL, NULL, NULL, NULL);
ce426f
 	  if (n >= 0)
ce426f
 	    goto got_it_already;
ce426f
 	}
ce426f
@@ -481,14 +483,14 @@ _nss_dns_gethostbyaddr2_r (const void *a
ce426f
     }
ce426f
 
ce426f
   n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
ce426f
-			 1024, &host_buffer.ptr, NULL, NULL, NULL);
ce426f
+			 1024, &host_buffer.ptr, NULL, NULL, NULL, NULL);
ce426f
   if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0)
ce426f
     {
ce426f
       strcpy (qp, "ip6.int");
ce426f
       n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
ce426f
 			     host_buffer.buf != orig_host_buffer
ce426f
 			     ? MAXPACKET : 1024, &host_buffer.ptr,
ce426f
-			     NULL, NULL, NULL);
ce426f
+			     NULL, NULL, NULL, NULL);
ce426f
     }
ce426f
   if (n < 0)
ce426f
     {
ce426f
Index: glibc-2.17-c758a686/resolv/nss_dns/dns-network.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/resolv/nss_dns/dns-network.c
ce426f
+++ glibc-2.17-c758a686/resolv/nss_dns/dns-network.c
ce426f
@@ -129,7 +129,7 @@ _nss_dns_getnetbyname_r (const char *nam
ce426f
   net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
ce426f
 
ce426f
   anslen = __libc_res_nsearch (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
ce426f
-			       1024, &net_buffer.ptr, NULL, NULL, NULL);
ce426f
+			       1024, &net_buffer.ptr, NULL, NULL, NULL, NULL);
ce426f
   if (anslen < 0)
ce426f
     {
ce426f
       /* Nothing found.  */
ce426f
@@ -205,7 +205,7 @@ _nss_dns_getnetbyaddr_r (uint32_t net, i
ce426f
   net_buffer.buf = orig_net_buffer = (querybuf *) alloca (1024);
ce426f
 
ce426f
   anslen = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, net_buffer.buf->buf,
ce426f
-			      1024, &net_buffer.ptr, NULL, NULL, NULL);
ce426f
+			      1024, &net_buffer.ptr, NULL, NULL, NULL, NULL);
ce426f
   if (anslen < 0)
ce426f
     {
ce426f
       /* Nothing found.  */
ce426f
Index: glibc-2.17-c758a686/resolv/res_query.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/resolv/res_query.c
ce426f
+++ glibc-2.17-c758a686/resolv/res_query.c
ce426f
@@ -98,7 +98,7 @@ static int
ce426f
 __libc_res_nquerydomain(res_state statp, const char *name, const char *domain,
ce426f
 			int class, int type, u_char *answer, int anslen,
ce426f
 			u_char **answerp, u_char **answerp2, int *nanswerp2,
ce426f
-			int *resplen2);
ce426f
+			int *resplen2, int *answerp2_malloced);
ce426f
 
ce426f
 /*
ce426f
  * Formulate a normal query, send, and await answer.
ce426f
@@ -119,7 +119,8 @@ __libc_res_nquery(res_state statp,
ce426f
 		  u_char **answerp,	/* if buffer needs to be enlarged */
ce426f
 		  u_char **answerp2,
ce426f
 		  int *nanswerp2,
ce426f
-		  int *resplen2)
ce426f
+		  int *resplen2,
ce426f
+		  int *answerp2_malloced)
ce426f
 {
ce426f
 	HEADER *hp = (HEADER *) answer;
ce426f
 	HEADER *hp2;
ce426f
@@ -224,7 +225,8 @@ __libc_res_nquery(res_state statp,
ce426f
 	}
ce426f
 	assert (answerp == NULL || (void *) *answerp == (void *) answer);
ce426f
 	n = __libc_res_nsend(statp, query1, nquery1, query2, nquery2, answer,
ce426f
-			     anslen, answerp, answerp2, nanswerp2, resplen2);
ce426f
+			     anslen, answerp, answerp2, nanswerp2, resplen2,
ce426f
+			     answerp2_malloced);
ce426f
 	if (use_malloc)
ce426f
 		free (buf);
ce426f
 	if (n < 0) {
ce426f
@@ -316,7 +318,7 @@ res_nquery(res_state statp,
ce426f
 	   int anslen)		/* size of answer buffer */
ce426f
 {
ce426f
 	return __libc_res_nquery(statp, name, class, type, answer, anslen,
ce426f
-				 NULL, NULL, NULL, NULL);
ce426f
+				 NULL, NULL, NULL, NULL, NULL);
ce426f
 }
ce426f
 libresolv_hidden_def (res_nquery)
ce426f
 
ce426f
@@ -335,7 +337,8 @@ __libc_res_nsearch(res_state statp,
ce426f
 		   u_char **answerp,
ce426f
 		   u_char **answerp2,
ce426f
 		   int *nanswerp2,
ce426f
-		   int *resplen2)
ce426f
+		   int *resplen2,
ce426f
+		   int *answerp2_malloced)
ce426f
 {
ce426f
 	const char *cp, * const *domain;
ce426f
 	HEADER *hp = (HEADER *) answer;
ce426f
@@ -360,7 +363,7 @@ __libc_res_nsearch(res_state statp,
ce426f
 	if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
ce426f
 		return (__libc_res_nquery(statp, cp, class, type, answer,
ce426f
 					  anslen, answerp, answerp2,
ce426f
-					  nanswerp2, resplen2));
ce426f
+					  nanswerp2, resplen2, answerp2_malloced));
ce426f
 
ce426f
 #ifdef DEBUG
ce426f
 	if (statp->options & RES_DEBUG)
ce426f
@@ -377,7 +380,8 @@ __libc_res_nsearch(res_state statp,
ce426f
 	if (dots >= statp->ndots || trailing_dot) {
ce426f
 		ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
ce426f
 					      answer, anslen, answerp,
ce426f
-					      answerp2, nanswerp2, resplen2);
ce426f
+					      answerp2, nanswerp2, resplen2,
ce426f
+					      answerp2_malloced);
ce426f
 		if (ret > 0 || trailing_dot
ce426f
 		    /* If the second response is valid then we use that.  */
ce426f
 		    || (ret == 0 && resplen2 != NULL && *resplen2 > 0))
ce426f
@@ -388,11 +392,11 @@ __libc_res_nsearch(res_state statp,
ce426f
 			answer = *answerp;
ce426f
 			anslen = MAXPACKET;
ce426f
 		}
ce426f
-		if (answerp2
ce426f
-		    && (*answerp2 < answer || *answerp2 >= answer + anslen))
ce426f
+		if (answerp2 && *answerp2_malloced)
ce426f
 		  {
ce426f
 		    free (*answerp2);
ce426f
 		    *answerp2 = NULL;
ce426f
+		    *answerp2_malloced = 0;
ce426f
 		  }
ce426f
 	}
ce426f
 
ce426f
@@ -419,7 +423,7 @@ __libc_res_nsearch(res_state statp,
ce426f
 						      class, type,
ce426f
 						      answer, anslen, answerp,
ce426f
 						      answerp2, nanswerp2,
ce426f
-						      resplen2);
ce426f
+						      resplen2, answerp2_malloced);
ce426f
 			if (ret > 0 || (ret == 0 && resplen2 != NULL
ce426f
 					&& *resplen2 > 0))
ce426f
 				return (ret);
ce426f
@@ -428,12 +432,11 @@ __libc_res_nsearch(res_state statp,
ce426f
 				answer = *answerp;
ce426f
 				anslen = MAXPACKET;
ce426f
 			}
ce426f
-			if (answerp2
ce426f
-			    && (*answerp2 < answer
ce426f
-				|| *answerp2 >= answer + anslen))
ce426f
+			if (answerp2 && *answerp2_malloced)
ce426f
 			  {
ce426f
 			    free (*answerp2);
ce426f
 			    *answerp2 = NULL;
ce426f
+			    *answerp2_malloced = 0;
ce426f
 			  }
ce426f
 
ce426f
 			/*
ce426f
@@ -489,7 +492,8 @@ __libc_res_nsearch(res_state statp,
ce426f
 	    && !(tried_as_is || root_on_list)) {
ce426f
 		ret = __libc_res_nquerydomain(statp, name, NULL, class, type,
ce426f
 					      answer, anslen, answerp,
ce426f
-					      answerp2, nanswerp2, resplen2);
ce426f
+					      answerp2, nanswerp2, resplen2,
ce426f
+					      answerp2_malloced);
ce426f
 		if (ret > 0 || (ret == 0 && resplen2 != NULL
ce426f
 				&& *resplen2 > 0))
ce426f
 			return (ret);
ce426f
@@ -502,10 +506,11 @@ __libc_res_nsearch(res_state statp,
ce426f
 	 * else send back meaningless H_ERRNO, that being the one from
ce426f
 	 * the last DNSRCH we did.
ce426f
 	 */
ce426f
-	if (answerp2 && (*answerp2 < answer || *answerp2 >= answer + anslen))
ce426f
+	if (answerp2 && *answerp2_malloced)
ce426f
 	  {
ce426f
 	    free (*answerp2);
ce426f
 	    *answerp2 = NULL;
ce426f
+	    *answerp2_malloced = 0;
ce426f
 	  }
ce426f
 	if (saved_herrno != -1)
ce426f
 		RES_SET_H_ERRNO(statp, saved_herrno);
ce426f
@@ -525,7 +530,7 @@ res_nsearch(res_state statp,
ce426f
 	    int anslen)		/* size of answer */
ce426f
 {
ce426f
 	return __libc_res_nsearch(statp, name, class, type, answer,
ce426f
-				  anslen, NULL, NULL, NULL, NULL);
ce426f
+				  anslen, NULL, NULL, NULL, NULL, NULL);
ce426f
 }
ce426f
 libresolv_hidden_def (res_nsearch)
ce426f
 
ce426f
@@ -543,7 +548,8 @@ __libc_res_nquerydomain(res_state statp,
ce426f
 			u_char **answerp,
ce426f
 			u_char **answerp2,
ce426f
 			int *nanswerp2,
ce426f
-			int *resplen2)
ce426f
+			int *resplen2,
ce426f
+			int *answerp2_malloced)
ce426f
 {
ce426f
 	char nbuf[MAXDNAME];
ce426f
 	const char *longname = nbuf;
ce426f
@@ -585,7 +591,7 @@ __libc_res_nquerydomain(res_state statp,
ce426f
 	}
ce426f
 	return (__libc_res_nquery(statp, longname, class, type, answer,
ce426f
 				  anslen, answerp, answerp2, nanswerp2,
ce426f
-				  resplen2));
ce426f
+				  resplen2, answerp2_malloced));
ce426f
 }
ce426f
 
ce426f
 int
ce426f
@@ -597,7 +603,8 @@ res_nquerydomain(res_state statp,
ce426f
 	    int anslen)		/* size of answer */
ce426f
 {
ce426f
 	return __libc_res_nquerydomain(statp, name, domain, class, type,
ce426f
-				       answer, anslen, NULL, NULL, NULL, NULL);
ce426f
+				       answer, anslen, NULL, NULL, NULL, NULL,
ce426f
+				       NULL);
ce426f
 }
ce426f
 libresolv_hidden_def (res_nquerydomain)
ce426f
 
ce426f
Index: glibc-2.17-c758a686/resolv/res_send.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/resolv/res_send.c
ce426f
+++ glibc-2.17-c758a686/resolv/res_send.c
ce426f
@@ -186,12 +186,12 @@ evNowTime(struct timespec *res) {
ce426f
 static int		send_vc(res_state, const u_char *, int,
ce426f
 				const u_char *, int,
ce426f
 				u_char **, int *, int *, int, u_char **,
ce426f
-				u_char **, int *, int *);
ce426f
+				u_char **, int *, int *, int *);
ce426f
 static int		send_dg(res_state, const u_char *, int,
ce426f
 				const u_char *, int,
ce426f
 				u_char **, int *, int *, int,
ce426f
 				int *, int *, u_char **,
ce426f
-				u_char **, int *, int *);
ce426f
+				u_char **, int *, int *, int *);
ce426f
 #ifdef DEBUG
ce426f
 static void		Aerror(const res_state, FILE *, const char *, int,
ce426f
 			       const struct sockaddr *);
ce426f
@@ -343,7 +343,7 @@ int
ce426f
 __libc_res_nsend(res_state statp, const u_char *buf, int buflen,
ce426f
 		 const u_char *buf2, int buflen2,
ce426f
 		 u_char *ans, int anssiz, u_char **ansp, u_char **ansp2,
ce426f
-		 int *nansp2, int *resplen2)
ce426f
+		 int *nansp2, int *resplen2, int *ansp2_malloced)
ce426f
 {
ce426f
   int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
ce426f
 
ce426f
@@ -546,7 +546,8 @@ __libc_res_nsend(res_state statp, const
ce426f
 			try = statp->retry;
ce426f
 			n = send_vc(statp, buf, buflen, buf2, buflen2,
ce426f
 				    &ans, &anssiz, &terrno,
ce426f
-				    ns, ansp, ansp2, nansp2, resplen2);
ce426f
+				    ns, ansp, ansp2, nansp2, resplen2,
ce426f
+				    ansp2_malloced);
ce426f
 			if (n < 0)
ce426f
 				return (-1);
ce426f
 			if (n == 0 && (buf2 == NULL || *resplen2 == 0))
ce426f
@@ -556,7 +557,7 @@ __libc_res_nsend(res_state statp, const
ce426f
 			n = send_dg(statp, buf, buflen, buf2, buflen2,
ce426f
 				    &ans, &anssiz, &terrno,
ce426f
 				    ns, &v_circuit, &gotsomewhere, ansp,
ce426f
-				    ansp2, nansp2, resplen2);
ce426f
+				    ansp2, nansp2, resplen2, ansp2_malloced);
ce426f
 			if (n < 0)
ce426f
 				return (-1);
ce426f
 			if (n == 0 && (buf2 == NULL || *resplen2 == 0))
ce426f
@@ -646,7 +647,7 @@ res_nsend(res_state statp,
ce426f
 	  const u_char *buf, int buflen, u_char *ans, int anssiz)
ce426f
 {
ce426f
   return __libc_res_nsend(statp, buf, buflen, NULL, 0, ans, anssiz,
ce426f
-			  NULL, NULL, NULL, NULL);
ce426f
+			  NULL, NULL, NULL, NULL, NULL);
ce426f
 }
ce426f
 libresolv_hidden_def (res_nsend)
ce426f
 
ce426f
@@ -657,7 +658,7 @@ send_vc(res_state statp,
ce426f
 	const u_char *buf, int buflen, const u_char *buf2, int buflen2,
ce426f
 	u_char **ansp, int *anssizp,
ce426f
 	int *terrno, int ns, u_char **anscp, u_char **ansp2, int *anssizp2,
ce426f
-	int *resplen2)
ce426f
+	int *resplen2, int *ansp2_malloced)
ce426f
 {
ce426f
 	const HEADER *hp = (HEADER *) buf;
ce426f
 	const HEADER *hp2 = (HEADER *) buf2;
ce426f
@@ -823,6 +824,8 @@ send_vc(res_state statp,
ce426f
 			}
ce426f
 			*thisanssizp = MAXPACKET;
ce426f
 			*thisansp = newp;
ce426f
+			if (thisansp == ansp2)
ce426f
+			  *ansp2_malloced = 1;
ce426f
 			anhp = (HEADER *) newp;
ce426f
 			len = rlen;
ce426f
 		} else {
ce426f
@@ -992,7 +995,7 @@ send_dg(res_state statp,
ce426f
 	const u_char *buf, int buflen, const u_char *buf2, int buflen2,
ce426f
 	u_char **ansp, int *anssizp,
ce426f
 	int *terrno, int ns, int *v_circuit, int *gotsomewhere, u_char **anscp,
ce426f
-	u_char **ansp2, int *anssizp2, int *resplen2)
ce426f
+	u_char **ansp2, int *anssizp2, int *resplen2, int *ansp2_malloced)
ce426f
 {
ce426f
 	const HEADER *hp = (HEADER *) buf;
ce426f
 	const HEADER *hp2 = (HEADER *) buf2;
ce426f
@@ -1235,6 +1238,8 @@ send_dg(res_state statp,
ce426f
 			if (newp != NULL) {
ce426f
 				*anssizp = MAXPACKET;
ce426f
 				*thisansp = ans = newp;
ce426f
+				if (thisansp == ansp2)
ce426f
+				  *ansp2_malloced = 1;
ce426f
 			}
ce426f
 		}
ce426f
 		HEADER *anhp = (HEADER *) *thisansp;