]> The Tcpdump Group git mirrors - libpcap/commit
Do all shifts in SWAPSHORT() and SWAPLONG() on unsigned values.
authorGuy Harris <[email protected]>
Fri, 22 Jun 2018 06:34:53 +0000 (23:34 -0700)
committerGuy Harris <[email protected]>
Fri, 22 Jun 2018 06:34:53 +0000 (23:34 -0700)
commitc0c6e23ee5f625a59c560522ec000a9b00fee67a
treefc574d8ed0f904304efbc7d83c3e901cfd4c15ee
parentbe18c1521d1436be38a7024ada3ddef95f412554
Do all shifts in SWAPSHORT() and SWAPLONG() on unsigned values.

To quote C99 section 6.5.7 "Bitwise shift operators":

The result of E1 << E2 is E1 left-shifted E2 bit positions;
vacated bits are filled with zeros.  ...  If E1 has a signed
type and nonnegative value, and E1 x 2^E2 is representable in
the result type, then that is the resulting value; otherwise,
the behavior is undefined.

In practice, signed vs. unsigned shouldn't make a difference, as long
as we don't get a trap of some sort in the "isn't representable" case,
but we might as well do an unsigned shift, because we're just doing
bit-banging, so we *want* unsigned - yeah, it means the result may not
have the same signedness as the value we pass in, but, as the saying
goes, bits are bits.  We end up stuffing the result back into the place
from which we extracted it, so it ultimately will get back the correct
signedness.

This should keep UBSAN quiet.

While we're at it, in SWAPSHORT(), do all the bit-banging on u_int
values, and cast the result to u_short.
pcap-common.h