Commit a1d62691 authored by Tom Jackson's avatar Tom Jackson Committed by Facebook Github Bot

Fix UB in folly/experimental/Bits.h

Summary:
Test output before the fix is applied:
```
folly/experimental/Bits.h:247:59: runtime error: signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'
     #0 folly/experimental/Bits.h:247 folly::Bits<...>::set(int*, unsigned long, unsigned long, int)
     #1 folly/experimental/test/BitsTest.cpp:228 std::enable_if<...>::type (anonymous namespace)::testSet<...>(unsigned char*, unsigned long, unsigned long, int)
     #2 folly/experimental/test/BitsTest.cpp:263 Bits_Boundaries_Test::TestBody()
    #17 folly/experimental/test/BitsTest.cpp:381 main
```

Reviewed By: philippv

Differential Revision: D5160789

fbshipit-source-id: 43f1926d58f1a5c019d4f8794d10a7a80a5c4749
parent d53c91d0
......@@ -242,8 +242,7 @@ inline void Bits<T, Traits>::set(T* p, size_t bitStart, size_t count,
size_t countInThisBlock = bitsPerBlock - offset;
size_t countInNextBlock = count - countInThisBlock;
UnderlyingType thisBlock =
UnderlyingType(value & ((one << countInThisBlock) - 1));
UnderlyingType thisBlock = UnderlyingType(value & ones(countInThisBlock));
UnderlyingType nextBlock = UnderlyingType(value >> countInThisBlock);
if (std::is_signed<UnderlyingType>::value) {
nextBlock &= ones(countInNextBlock);
......
......@@ -255,6 +255,17 @@ T testValue(int bits) {
}
} // anonymous namespace
TEST(Bits, Boundaries) {
uint8_t buf[20];
for (size_t offset = 0; offset <= 64; ++offset) {
for (size_t size = 0; size <= 32; ++size) {
int32_t value = testValue<int32_t>(size);
testSet<true>(buf, offset, size, value);
EXPECT_EQ(value, (testGet<true, int32_t>(buf, offset, size)));
}
}
}
template <size_t N>
void accSize(size_t& w) {
for (size_t s = 0; s <= N; ++s) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment