From 0d49e47978d5f2d0b108b0ddc65943763e88f729 Mon Sep 17 00:00:00 2001 From: "William A. Kennington III" Date: Mon, 8 Apr 2019 20:27:26 -0700 Subject: message/unpack: Fix undefined behavior It's UB to shift an integer by the size of that integer. More specifically, if you disable compiler optimization and try and unpack a 32 bit bitset you will end up with a 0x0 mask. Avoid UB by replacing shift subtract with a negate shift. Tested: Unit tests pass now. Change-Id: I03a6f866a51c955b57787d641da9180841747e4c Signed-off-by: William A. Kennington III --- include/ipmid/message/unpack.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/ipmid/message/unpack.hpp b/include/ipmid/message/unpack.hpp index d96928f..94f80f1 100644 --- a/include/ipmid/message/unpack.hpp +++ b/include/ipmid/message/unpack.hpp @@ -207,7 +207,9 @@ struct UnpackSingle> { return -1; } - fixed_uint_t bitmask = ((1 << count) - 1); + fixed_uint_t bitmask = + ~fixed_uint_t(0) >> + (details::bitStreamSize - count); t |= (p.bitStream & bitmask).convert_to(); p.bitStream >>= count; p.bitCount -= count; -- cgit v1.2.1