Fix UB: Make read_int() follow SEI CERT INT13-C and INT34-C.master
commit8f0dd4c8788a94a3b3841207ac96662f5ce30d53
authorPrzemyslaw Pawelczyk <przemoc@gmail.com>
Wed, 1 Feb 2023 22:31:06 +0000 (1 23:31 +0100)
committerPrzemyslaw Pawelczyk <przemoc@gmail.com>
Wed, 1 Feb 2023 22:31:06 +0000 (1 23:31 +0100)
tree51790559e22600e44868408f5a9df14b9014f27f
parent107158f9a13720dfe2f8fa9ff97a776a6b34da1b
Fix UB: Make read_int() follow SEI CERT INT13-C and INT34-C.

Follow:
- INT13-C. Use bitwise operators only on unsigned operands
- INT34-C. Do not shift an expression by a negative number of bits or by
  greater than or equal to the number of bits that exist in the operand
to fix undefined behavior when read_int() reads int larger than 4 bytes,
which is part of apply and dump stored metadata flows.

Issue can be spotted with -fsanitize=undefined.

    src/utils.c:160:34: runtime error: shift exponent 32 is too large for 32-bit type 'int'
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/utils.c:160:34 in

It does not fix possible UB for reading integers larger than 8 bytes,
but there are no such use cases right now.
src/utils.c