diff options
author | pb <pb@FreeBSD.org> | 1999-11-15 20:04:53 +0000 |
---|---|---|
committer | pb <pb@FreeBSD.org> | 1999-11-15 20:04:53 +0000 |
commit | 4dcdfee42a3ffb9def479161d2e2ab80b5162267 (patch) | |
tree | fa6369b19a12b8c2b2309ae0d02395e84bb77599 /sbin/ping/ping.c | |
parent | ac2cbe7bd7b4cd82c4b592681b966a773eb7fcab (diff) | |
download | FreeBSD-src-4dcdfee42a3ffb9def479161d2e2ab80b5162267.zip FreeBSD-src-4dcdfee42a3ffb9def479161d2e2ab80b5162267.tar.gz |
Fix aliasing bug causing in_cksum() to fail on odd packet sizes
due to compiler optimization.
PR: bin/13292
Suggested by: wollman
Diffstat (limited to 'sbin/ping/ping.c')
-rw-r--r-- | sbin/ping/ping.c | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index edbc82a..f96774d 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -929,7 +929,10 @@ in_cksum(addr, len) register int nleft = len; register u_short *w = addr; register int sum = 0; - u_short answer = 0; + union { + u_int16_t us; + u_int8_t uc[2]; + } answer; /* * Our algorithm is simple, using a 32 bit accumulator (sum), we add @@ -943,15 +946,16 @@ in_cksum(addr, len) /* mop up an odd byte, if necessary */ if (nleft == 1) { - *(u_char *)(&answer) = *(u_char *)w ; - sum += answer; + answer.uc[0] = *(u_char *)w; + answer.uc[1] = 0; + sum += answer.us; } /* add back carry outs from top 16 bits to low 16 bits */ sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ - answer = ~sum; /* truncate to 16 bits */ - return(answer); + answer.us = ~sum; /* truncate to 16 bits */ + return(answer.us); } /* |