diff options
author | des <des@FreeBSD.org> | 2004-07-06 09:22:18 +0000 |
---|---|---|
committer | des <des@FreeBSD.org> | 2004-07-06 09:22:18 +0000 |
commit | c05f2ebe921742f142496c086120ee8a02d16b90 (patch) | |
tree | 07a2be27b9b9801ab65c5c3c5c01c3f0a04290a9 | |
parent | 3a60494e7441500d46a926d8bc5110e93cda5667 (diff) | |
download | FreeBSD-src-c05f2ebe921742f142496c086120ee8a02d16b90.zip FreeBSD-src-c05f2ebe921742f142496c086120ee8a02d16b90.tar.gz |
Rewrite twowords() to access its argument through a char pointer and not
a short pointer. The previous implementation seems to be in a gray zone
of the C standard, and GCC generates incorrect code for it at -O2 or
higher on some platforms.
-rw-r--r-- | lib/libalias/alias.c | 13 | ||||
-rw-r--r-- | sys/netinet/libalias/alias.c | 13 |
2 files changed, 20 insertions, 6 deletions
diff --git a/lib/libalias/alias.c b/lib/libalias/alias.c index 8321900..6acf03b 100644 --- a/lib/libalias/alias.c +++ b/lib/libalias/alias.c @@ -139,9 +139,16 @@ __FBSDID("$FreeBSD$"); static __inline int twowords(void *p) { - u_short *s = p; - - return (s[0] + s[1]); + uint8_t *c = p; + +#if BYTE_ORDER == LITTLE_ENDIAN + uint16_t s1 = ((uint16_t)c[1] << 8) + (uint16_t)c[0]; + uint16_t s2 = ((uint16_t)c[3] << 8) + (uint16_t)c[2]; +#else + uint16_t s1 = ((uint16_t)c[0] << 8) + (uint16_t)c[1]; + uint16_t s2 = ((uint16_t)c[2] << 8) + (uint16_t)c[3]; +#endif + return (s1 + s2); } /* TCP Handling Routines diff --git a/sys/netinet/libalias/alias.c b/sys/netinet/libalias/alias.c index 8321900..6acf03b 100644 --- a/sys/netinet/libalias/alias.c +++ b/sys/netinet/libalias/alias.c @@ -139,9 +139,16 @@ __FBSDID("$FreeBSD$"); static __inline int twowords(void *p) { - u_short *s = p; - - return (s[0] + s[1]); + uint8_t *c = p; + +#if BYTE_ORDER == LITTLE_ENDIAN + uint16_t s1 = ((uint16_t)c[1] << 8) + (uint16_t)c[0]; + uint16_t s2 = ((uint16_t)c[3] << 8) + (uint16_t)c[2]; +#else + uint16_t s1 = ((uint16_t)c[0] << 8) + (uint16_t)c[1]; + uint16_t s2 = ((uint16_t)c[2] << 8) + (uint16_t)c[3]; +#endif + return (s1 + s2); } /* TCP Handling Routines |