From a3a89d7c366c2fd2b35b63ecd1532e233a2e9acb Mon Sep 17 00:00:00 2001 From: Andreas Gustafsson Date: Fri, 2 Nov 2001 20:35:28 +0000 Subject: [PATCH] pullup: 1090. [bug] libbind: dns_ho.c:add_hostent() was not returning the amount of memory consumed resulting in garbage address being returned. Alignment calculations were wasting space. We wern't suppressing duplicate addresses. --- CHANGES | 6 ++++++ lib/bind/irs/dns_ho.c | 34 +++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/CHANGES b/CHANGES index 4ba429a5b06..7e4bdb6b308 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,12 @@ 1101. [bug] Array bounds read error in lwres_gai_strerror. +1090. [bug] libbind: dns_ho.c:add_hostent() was not returning + the amount of memory consumed resulting in garbage + address being returned. Alignment calculations were + wasting space. We weren't suppressing duplicate + addresses. + 1088. [port] libbind: MPE/iX C.70 (incomplete) 1086. [port] libbind: sunos: old sprintf. diff --git a/lib/bind/irs/dns_ho.c b/lib/bind/irs/dns_ho.c index 3e596f01671..a563ff746cd 100644 --- a/lib/bind/irs/dns_ho.c +++ b/lib/bind/irs/dns_ho.c @@ -52,7 +52,7 @@ /* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */ #if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: dns_ho.c,v 1.5 2001/07/02 00:44:50 marka Exp $"; +static const char rcsid[] = "$Id: dns_ho.c,v 1.5.2.1 2001/11/02 20:35:28 gson Exp $"; #endif /* LIBC_SCCS and not lint */ /* Imports. */ @@ -1400,7 +1400,8 @@ gethostans(struct irs_ho *this, buflen -= nn; } /* Ensure alignment. */ - bp += sizeof(align) - ((u_long)bp % sizeof(align)); + bp = (char *)(((u_long)bp + (sizeof(align) - 1)) & + ~(sizeof(align) - 1)); /* Avoid overflows. */ if (bp + n >= &pvt->hostbuf[sizeof pvt->hostbuf]) { had_error++; @@ -1421,6 +1422,8 @@ gethostans(struct irs_ho *this, had_error++; break; } + if (m == 0) + continue; if (hap < &pvt->h_addr_ptrs[MAXADDRS-1]) hap++; @@ -1491,6 +1494,8 @@ add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai) { int addrlen; char *addrp; + const char **tap; + char *obp = bp; switch(ai->ai_addr->sa_family) { case AF_INET6: @@ -1505,26 +1510,26 @@ add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai) return(-1); /* abort? */ } - bp += sizeof(align) - ((u_long)bp % sizeof(align)); + /* Ensure alignment. */ + bp = (char *)(((u_long)bp + (sizeof(align) - 1)) & + ~(sizeof(align) - 1)); + /* Avoid overflows. */ if (bp + addrlen >= &pvt->hostbuf[sizeof pvt->hostbuf]) return(-1); if (hap >= &pvt->h_addr_ptrs[MAXADDRS-1]) - return(addrlen); /* fail, but not treat it as an error. */ -#if 0 + return(0); /* fail, but not treat it as an error. */ + /* Suppress duplicates. */ for (tap = (const char **)pvt->h_addr_ptrs; *tap != NULL; tap++) - if (memcmp(*tap, cp, n) == 0) + if (memcmp(*tap, addrp, addrlen) == 0) break; - if (*tap != NULL) { - cp += n; - continue; - } -#endif + if (*tap != NULL) + return (0); memcpy(*hap = bp, addrp, addrlen); - return(addrlen); + return((bp + addrlen) - obp); } static void @@ -1536,7 +1541,10 @@ map_v4v6_hostent(struct hostent *hp, char **bpp, int *lenp) { hp->h_addrtype = AF_INET6; hp->h_length = IN6ADDRSZ; for (ap = hp->h_addr_list; *ap; ap++) { - int i = sizeof(align) - ((u_long)*bpp % sizeof(align)); + int i = (u_long)*bpp % sizeof(align); + + if (i != 0) + i = sizeof(align) - i; if (*lenp < (i + IN6ADDRSZ)) { /* Out of memory. Truncate address list here. */ -- 2.47.3