Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 23 Jun 2002 21:06:15 +0200
From:      Bernd Walter <ticso@cicely5.cicely.de>
To:        Christian Weisgerber <naddy@mips.inka.de>
Cc:        freebsd-alpha@FreeBSD.ORG
Subject:   Re: -CURRENT: UA in locate
Message-ID:  <20020623190614.GW53233@cicely5.cicely.de>
In-Reply-To: <af4u6m$21cu$1@kemoauc.mips.inka.de>
References:  <af4u6m$21cu$1@kemoauc.mips.inka.de>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Jun 23, 2002 at 04:49:26PM +0000, Christian Weisgerber wrote:
> The locate(1) command throws unaligned access errors in -CURRENT.
> This has probably been triggered by the switch to gcc 3.1.
> 
> Here's a diff with extended context to show what's going on:
> 
> Index: util.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/locate/locate/util.c,v
> retrieving revision 1.9
> diff -u -U9 -r1.9 util.c
> --- util.c	22 Mar 2002 01:22:48 -0000	1.9
> +++ util.c	23 Jun 2002 16:33:24 -0000
> @@ -225,19 +225,19 @@
>   * Convert network byte order to host byte order if neccessary. 
>   * So we can read on FreeBSD/i386 (little endian) a locate database
>   * which was built on SunOS/sparc (big endian).
>   */
>  
>  int 
>  getwm(p)
>  	caddr_t p;
>  {
> -	static char buf[INTSIZE];
> +	char buf[INTSIZE];
>  	register int i;
>  
>  	for (i = 0; i < INTSIZE; i++)
>  		buf[i] = *p++;
>  
>  	i = *(int *)buf;
>  
>  	if (i > MAXPATHLEN || i < -(MAXPATHLEN)) {
>  		i = ntohl(i);
> ===================================================================
> 
> Apparently the author assumed that buf[] would be properly aligned,
> so that dereferencing an integer pointer would be safe.  I'm at a
> loss how somebody can arrive at this assumption for a static object.
> For an automatic variable on the other hand it is a reasonable
> guess.
> 
> Is that fix okay, or should we play games with "int buf; (char *)&buf"?

The fix doesn't change the alignment contraints.
Either make it a union with a stronger aligned type or malloc() it.
But malloc'ing just an int is silly.
It's used as an int later so it should be either be a union with an
int or just an int and cast inside the loop as you already wrote.

-- 
B.Walter              COSMO-Project         http://www.cosmo-project.de
ticso@cicely.de         Usergroup           info@cosmo-project.de


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-alpha" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020623190614.GW53233>