[Pdns-users] Questions redux
Chris Andrews
chris at nodnol.org
Sun Sep 7 12:45:35 UTC 2003
On Sun, Sep 07, 2003 at 02:07:48PM +0200, Norbert Sendetzky wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Saturday 06 September 2003 22:30, Chris Andrews wrote:
> > I've tried pdns on Solaris (SPARC, 32bit on a sun4m), and I found
> > that it generated a large number of SIGBUS errors due to unaligned
> > accesses (the SPARC being more restrictive than i386) I flagged up
> > a number of these, which were fixed, but I think some remain.
>
> Can you give me examples of bad code? I want to prevent this this in
> my code, but I don't know how to do this.
Here's an example, long since fixed in pdns (from dnspacket.cc):
rr.content+=" ";rr.content+=itoa(ntohl(*(unsigned int *)(datapos+offset)));
At this point, unless datapos+offset evaluates to a 4 byte aligned
location, we get SIGBUS, because on SPARC, long reads must be aligned
on 4 byte boundaries[1].
This code was replaced with:
rr.content+=" ";rr.content+=itoa(getLong( datapos+offset ));
where getLong is:
u_int32_t getLong(const unsigned char* p)
{
return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3];
}
This extracts the long value from the packet as four separate single
byte reads rather than one 4 byte read, and so can be used without
concern for alignment.
Hope that helps
Chris.
[1] http://www.sparc.com/standards/V8.pdf (the 32 bit SPARC manual)
More information about the Pdns-users
mailing list