Fix aliasing bug causing in_cksum() to fail on odd packet sizes

due to compiler optimization.

PR:		bin/13292
Suggested by:	wollman
This commit is contained in:
Pierre Beyssac 1999-11-15 20:04:53 +00:00
parent 309c48c65b
commit 3ec12efecc
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=53191

View File

@ -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);
}
/*