NetBSD Problem Report #10048

Received: (qmail 19761 invoked from network); 5 May 2000 00:21:11 -0000
Message-Id: <200005050020.RAA24777@jtc.redback.com>
Date: Thu, 4 May 2000 17:20:41 -0700 (PDT)
From: <jtc@redback.com>
Reply-To: jtc@redback.com
To: gnats-bugs@gnats.netbsd.org
Subject: /etc/hosts lookup failure
X-Send-Pr-Version: 3.95

>Number:         10048
>Category:       lib
>Synopsis:       /etc/hosts lookup failure
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    lib-bug-people
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri May 05 00:22:00 +0000 2000
>Closed-Date:    Tue May 23 07:04:18 +0000 2000
>Last-Modified:  Tue May 23 07:04:18 +0000 2000
>Originator:     
>Release:        NetBSD-current, 2000-05-01
>Organization:
RedBack Networks
>Environment:
System: NetBSD powermac 1.4X NetBSD 1.4X (GENERIC) #0: Wed May  3 09:19:49 PDT 2000     jtc@powermac:/usr/src/sys/arch/macppc/compile/GENERIC macppc

>Description:
After installing NetBSD on my powermac, I discovered some programs
were not able to look up IP addresses from hostnames in /etc/hosts
properly.  Using tcpdump, I discovered that the programs that failed
were using bogus addresses.  The addresses for a given host were not
consistant.

I tracked this down to _gethtbyname2() in gethnamaddr.c.  When the
function finds the first matching host entry, it allocates a temporary
buffer for the host name, the host aliases, and MAXADDRS addresses. It
copies the host name and aliases to the buffer, and then aligns the
pointer before appending the host address.  For subsequent matching
host entries, the host address is simply appended to the buffer.

After all the host entries have been processed, the temporary buffer
is copied to a static buffer and freed.  The buffer is then unpacked
and pointers to the host name, the host aliases, and addresses, are
extracted.  Like when the buffer was originally constructed, after 
the host name and host aliases are processed, the pointer is aligned
before the host addresses are processed.

The problem arises because the temporary buffer allocated with
malloc() and the static buffer don't necessarily have the same
alignment.  In this case, the temp buffer is going to be aligned on
an 8 byte boundry, and the static buffer may have any alignment.

I solved my immediate problem by commenting out the two lines that
align the pointer.  This may not be the most applicable solution.  I
assume the alignment is present for the combination of architectures
that don't support misaligned accesses and programs that dereference
hp->h_addr directly instead of treating it as opaque data and copies
the hp->h_length bytes at hp->h_addr.

If this is important, this could be fixed by changing:
	ptr = memcpy(hostbuf, tmpbuf, len);

To something like this:
	ptr = (char *) ALIGN(hostbuf);
	memcpy (ptr, tmpbuf, len);


>How-To-Repeat:

>Fix:
*** gethnamaddr.c~	Wed Apr 26 03:13:05 2000
--- gethnamaddr.c	Thu May  4 14:53:04 2000
***************
*** 891,898 ****
  				while ((*ptr++ = *src++) != '\0');
  			}
  			*ptr++ = '\0';
! 
  			ptr = (char *)(void *)ALIGN(ptr);
  		}

  		(void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
--- 891,899 ----
  				while ((*ptr++ = *src++) != '\0');
  			}
  			*ptr++ = '\0';
! #if 0
  			ptr = (char *)(void *)ALIGN(ptr);
+ #endif
  		}

  		(void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
***************
*** 923,929 ****
--- 924,932 ----
  	ptr++;
  	*cp = NULL;

+ #if 0
  	ptr = (char *)(void *)ALIGN(ptr);
+ #endif
  	cp = h_addr_ptrs;
  	while (num--) {
  		*cp++ = ptr;

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->feedback 
State-Changed-By: tron 
State-Changed-When: Sun May 14 14:56:54 PDT 2000 
State-Changed-Why:  
Please tell me if the following fix solves your problem: 

Index: gethnamaddr.c 
=================================================================== 
RCS file: /cvsroot/basesrc/lib/libc/net/gethnamaddr.c,v 
retrieving revision 1.32 
diff -u -r1.32 gethnamaddr.c 
--- gethnamaddr.c	2000/04/25 13:47:38	1.32 
+++ gethnamaddr.c	2000/05/14 21:56:53 
@@ -903,13 +903,13 @@ 
if (num == 0) return NULL; 

len = ptr - tmpbuf; 
-	if (len > sizeof(hostbuf)) { 
+	if (len > (sizeof(hostbuf) - ALIGNBYTES)) { 
free(tmpbuf); 
errno = ENOSPC; 
h_errno = NETDB_INTERNAL; 
return NULL; 
} 
-	ptr = memcpy(hostbuf, tmpbuf, len); 
+	ptr = memcpy(ALIGN(hostbuf), tmpbuf, len); 
free(tmpbuf); 

host.h_name = ptr; 
State-Changed-From-To: feedback->closed 
State-Changed-By: tron 
State-Changed-When: Tue May 23 00:03:51 PDT 2000 
State-Changed-Why:  
Submitter confirms that patch is functional. 
>Unformatted:
 J.T. Conklin

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.