[En-Nut-Discussion] Aligned short fetches don't work on ARM

Ben Hoyt benhoyt at gmail.com
Tue Sep 19 09:50:27 CEST 2006


Hi guys,

I noticed there are a couple of unaligned fetches in the IP code that may
cause problems on the ARM (ARMs only allow 16- and 32-bit memory accesses
from aligned addresses -- you get an exception if you don't). I found
similar fetches in some code I was working on (they were causing crashes
:-), and I thought I'd check the NutOS code base too.

The main culprit's on line 278 of net/tcpsm.c (
http://ethernut.cvs.sourceforge.net/ethernut/nut/net/tcpsm.c?view=markup#l_278
):

s = ntohs(*(u_short*) (cp + 2));

where cp is a character pointer which I believe isn't necessarily 16-bit
aligned. Something like the following is necessary:

s = (cp[2]<<8) + cp[3];

where I've fetched it directly from network order, but byte-by-byte.

The other instance is on line 168 of net/pppin.c (
http://ethernut.cvs.sourceforge.net/ethernut/nut/net/pppin.c?view=markup#l_168
):

protocol = ntohs(*(u_short *) nb->nb_dl.vp);

where the alignment of vp depends on how the compiler aligns/pads
structures, but may well not be aligned. Should be something like:

protocol = (((u_char *)nb->nb_dl.vp)[0]<<8) + ((u_char *)nb->nb_dl.vp)[1];

You could of course make a macro to fetch these things.

Cheers,
Ben

-- 
Ben Hoyt
Mobile: +64 21 331 841
Email: benhoyt at gmail.com



More information about the En-Nut-Discussion mailing list