]> The Tcpdump Group git mirrors - tcpdump/commitdiff
IEEE 802.15.4: Fix an undefined behavior at runtime
authorFrancois-Xavier Le Bail <[email protected]>
Thu, 28 Mar 2019 12:44:50 +0000 (13:44 +0100)
committerFrancois-Xavier Le Bail <[email protected]>
Thu, 28 Mar 2019 13:13:27 +0000 (14:13 +0100)
The error was:
print-802_15_4.c:442:9: runtime error: implicit conversion from type
'int' of value 15840046 (32-bit, signed) to type 'uint16_t' (aka
'unsigned short') changed the value to 45870 (16-bit, unsigned)

Proposal of fix by Guy Harris:
"Those shifted values will be converted (in the C abstract machine) to
int, but they'll all be in the range 0 to 65535, as will be the XOR of
all 4 of them; converting that to a uint16_t isn't undefined behavior."

print-802_15_4.c

index 54279ccaf9a54fb357e0d3c70425bbb522f19e2d..b1800e0862688dc1d4efefcd72fd906e376e869a 100644 (file)
@@ -439,10 +439,10 @@ ieee802_15_4_crc16(const u_char *p,
                /* Update CRC */
                x = crc >> 8 ^ y;
                x ^= x >> 4;
-               crc = (crc << 8) ^
-                       ((unsigned short)(x << 12)) ^
-                       ((unsigned short)(x <<5)) ^
-                       ((unsigned short)x);
+               crc = ((uint16_t)(crc << 8)) ^
+                       ((uint16_t)(x << 12)) ^
+                       ((uint16_t)(x << 5)) ^
+                       ((uint16_t)x);
                data_len--;
        }
        /* Reverse bits on output */