Module Name:    src
Committed By:   christos
Date:           Tue Sep 22 16:16:02 UTC 2015

Modified Files:
        src/lib/libc/net: gethnamaddr.c

Log Message:
- fix various leaks on error
- don't use the wrong error variable in switch
- always set the error return code
- return consistent errors when the input data cannot be handled.


To generate a diff of this commit:
cvs rdiff -u -r1.91 -r1.92 src/lib/libc/net/gethnamaddr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/net/gethnamaddr.c
diff -u src/lib/libc/net/gethnamaddr.c:1.91 src/lib/libc/net/gethnamaddr.c:1.92
--- src/lib/libc/net/gethnamaddr.c:1.91	Thu Jun 19 11:08:18 2014
+++ src/lib/libc/net/gethnamaddr.c	Tue Sep 22 12:16:02 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $	*/
+/*	$NetBSD: gethnamaddr.c,v 1.92 2015/09/22 16:16:02 christos Exp $	*/
 
 /*
  * ++Copyright++ 1985, 1988, 1993
@@ -57,7 +57,7 @@
 static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
 static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
 #else
-__RCSID("$NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $");
+__RCSID("$NetBSD: gethnamaddr.c,v 1.92 2015/09/22 16:16:02 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -244,6 +244,7 @@ getanswer(const querybuf *answer, int an
 		name_ok = res_dnok;
 		break;
 	default:
+		*he = NO_RECOVERY;
 		return NULL;	/* XXX should be abort(); */
 	}
 
@@ -964,6 +965,7 @@ _dns_gethtbyname(void *rv, void *cb_data
 	res = __res_get_state();
 	if (res == NULL) {
 		free(buf);
+		*info->he = NETDB_INTERNAL;
 		return NS_NOTFOUND;
 	}
 	n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf));
@@ -978,7 +980,7 @@ _dns_gethtbyname(void *rv, void *cb_data
 	free(buf);
 	__res_put_state(res);
 	if (hp == NULL)
-		switch (h_errno) {
+		switch (*info->he) {
 		case HOST_NOT_FOUND:
 			return NS_NOTFOUND;
 		case TRY_AGAIN:
@@ -1026,35 +1028,31 @@ _dns_gethtbyaddr(void *rv, void	*cb_data
 			    ((unsigned int)uaddr[n] >> 4) & 0xf);
 			if (advance > 0 && qp + advance < ep)
 				qp += advance;
-			else {
-				*info->he = NETDB_INTERNAL;
-				return NS_NOTFOUND;
-			}
-		}
-		if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
-			*info->he = NETDB_INTERNAL;
-			return NS_NOTFOUND;
+			else
+				goto norecovery;
 		}
+		if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf))
+			goto norecovery;
 		break;
 	default:
-		return NS_UNAVAIL;
+		goto norecovery;
 	}
 
 	buf = malloc(sizeof(*buf));
 	if (buf == NULL) {
-		*info->he = NETDB_INTERNAL;
-		return NS_NOTFOUND;
+		goto nospc;
 	}
 	res = __res_get_state();
 	if (res == NULL) {
 		free(buf);
-		return NS_NOTFOUND;
+		goto nospc;
 	}
 	n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
 	if (n < 0) {
 		free(buf);
 		debugprintf("res_nquery failed (%d)\n", res, n);
 		__res_put_state(res);
+		*info->he = HOST_NOT_FOUND;
 		return NS_NOTFOUND;
 	}
 	hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf,
@@ -1080,8 +1078,10 @@ _dns_gethtbyaddr(void *rv, void	*cb_data
 	hp->h_addr_list[1] = NULL;
 	(void)memcpy(bf, uaddr, (size_t)info->hp->h_length);
 	if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
-		if (blen + NS_IN6ADDRSZ > info->buflen)
+		if (blen + NS_IN6ADDRSZ > info->buflen) {
+			__res_put_state(res);
 			goto nospc;
+		}
 		map_v4v6_address(bf, bf);
 		hp->h_addrtype = AF_INET6;
 		hp->h_length = NS_IN6ADDRSZ;
@@ -1093,6 +1093,9 @@ _dns_gethtbyaddr(void *rv, void	*cb_data
 nospc:
 	*info->he = NETDB_INTERNAL;
 	return NS_UNAVAIL;
+norecovery:
+	*info->he = NO_RECOVERY;
+	return NS_UNAVAIL;
 }
 
 #ifdef YP

Reply via email to