$ clang-16 -fsanitize=undefined ./deserialize.c -o deserialize && ./deserialize
-1431655766
deserialize.c:10:20: runtime error: left shift of 170 by 24 places cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior deserialize.c:10:20 in
-1431655766
unsigned char* gets promoted to
int
, which cannot always be shifted by 24 bits.Justine Tunney blogs about it here: https://justine.lol/endian.html
Example:
Output: