mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Fix alignment issue of ones_complement_checksum
The ones_complement_checksum function assumes that the bytes passed into it are aligned on 16 bit boundaries. When using gcc (GCC) 6.2.1 20160916 (Red Hat 6.2.1-2) with -O2, this does not seem to hold true anymore; assuming 16 bit alignment will lead to accesses to uninitialized memory and wrong checksums. This commit adds a minimally invasive change that does not assume alignment anymore. This might have a small performance impact for every single packet we process. This error occured reproducibly when called from icmp6_checksum.
This commit is contained in:
parent
bd0a374c87
commit
24f74cb52e
1 changed files with 5 additions and 2 deletions
|
@ -18,13 +18,16 @@
|
||||||
// Returns the ones-complement checksum of a chunk of b short-aligned bytes.
|
// Returns the ones-complement checksum of a chunk of b short-aligned bytes.
|
||||||
int ones_complement_checksum(const void* p, int b, uint32 sum)
|
int ones_complement_checksum(const void* p, int b, uint32 sum)
|
||||||
{
|
{
|
||||||
const u_short* sp = (u_short*) p; // better be aligned!
|
const unsigned char* sp = (unsigned char*) p;
|
||||||
|
|
||||||
b /= 2; // convert to count of short's
|
b /= 2; // convert to count of short's
|
||||||
|
|
||||||
/* No need for endian conversions. */
|
/* No need for endian conversions. */
|
||||||
while ( --b >= 0 )
|
while ( --b >= 0 )
|
||||||
sum += *sp++;
|
{
|
||||||
|
sum += *sp + ( *(sp+1) << 8 );
|
||||||
|
sp += 2;
|
||||||
|
}
|
||||||
|
|
||||||
while ( sum > 0xffff )
|
while ( sum > 0xffff )
|
||||||
sum = (sum & 0xffff) + (sum >> 16);
|
sum = (sum & 0xffff) + (sum >> 16);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue