Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/net - fix various leaks on error



details:   https://anonhg.NetBSD.org/src/rev/39aa9937fddb
branches:  trunk
changeset: 340649:39aa9937fddb
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Sep 22 16:16:02 2015 +0000

description:
- 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.

diffstat:

 lib/libc/net/gethnamaddr.c |  35 +++++++++++++++++++----------------
 1 files changed, 19 insertions(+), 16 deletions(-)

diffs (109 lines):

diff -r 1c034348c590 -r 39aa9937fddb lib/libc/net/gethnamaddr.c
--- a/lib/libc/net/gethnamaddr.c        Tue Sep 22 16:15:08 2015 +0000
+++ b/lib/libc/net/gethnamaddr.c        Tue Sep 22 16:16:02 2015 +0000
@@ -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 @@
                name_ok = res_dnok;
                break;
        default:
+               *he = NO_RECOVERY;
                return NULL;    /* XXX should be abort(); */
        }
 
@@ -964,6 +965,7 @@
        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 @@
        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 @@
                            ((unsigned int)uaddr[n] >> 4) & 0xf);
                        if (advance > 0 && qp + advance < ep)
                                qp += advance;
-                       else {
-                               *info->he = NETDB_INTERNAL;
-                               return NS_NOTFOUND;
-                       }
+                       else
+                               goto norecovery;
                }
-               if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
-                       *info->he = NETDB_INTERNAL;
-                       return NS_NOTFOUND;
-               }
+               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 @@
        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 @@
 nospc:
        *info->he = NETDB_INTERNAL;
        return NS_UNAVAIL;
+norecovery:
+       *info->he = NO_RECOVERY;
+       return NS_UNAVAIL;
 }
 
 #ifdef YP



Home | Main Index | Thread Index | Old Index