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 <wak@google.com>
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 @@
{
return -1;
}
- fixed_uint_t<details::bitStreamSize> bitmask = ((1 << count) - 1);
+ fixed_uint_t<details::bitStreamSize> bitmask =
+ ~fixed_uint_t<details::bitStreamSize>(0) >>
+ (details::bitStreamSize - count);
t |= (p.bitStream & bitmask).convert_to<unsigned long long>();
p.bitStream >>= count;
p.bitCount -= count;