summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2019-04-08 20:27:26 -0700
committerWilliam A. Kennington III <wak@google.com>2019-04-11 18:40:25 -0700
commit0d49e47978d5f2d0b108b0ddc65943763e88f729 (patch)
tree73aaa9c1141a2e7425e92cb0ff660e0afcf7331e
parent11df4f6906edc0dfb23089a6e297158549c19ebd (diff)
downloadphosphor-host-ipmid-0d49e47978d5f2d0b108b0ddc65943763e88f729.tar.gz
phosphor-host-ipmid-0d49e47978d5f2d0b108b0ddc65943763e88f729.zip
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>
-rw-r--r--include/ipmid/message/unpack.hpp4
1 files changed, 3 insertions, 1 deletions
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<std::bitset<N>>
{
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;
OpenPOWER on IntegriCloud