Commit 322158b3 authored by Stella Lau's avatar Stella Lau Committed by Facebook Github Bot

Fix undefined behavior when decoding varint

Summary: Left shifting `0x7f` by `63` is undefined behavior (i.e. when decoding `0xFFFF...`)

Reviewed By: yfeldblum, terrelln

Differential Revision: D5653353

fbshipit-source-id: c74c9f43a9bc82d15a2223df853dc533cea1478b
parent bf29c7e0
......@@ -145,7 +145,7 @@ inline Expected<uint64_t, DecodeVarintError> tryDecodeVarint(Range<T*>& data) {
b = *p++; val |= (b & 0x7f) << 42; if (b >= 0) break;
b = *p++; val |= (b & 0x7f) << 49; if (b >= 0) break;
b = *p++; val |= (b & 0x7f) << 56; if (b >= 0) break;
b = *p++; val |= (b & 0x7f) << 63; if (b >= 0) break;
b = *p++; val |= (b & 0x01) << 63; if (b >= 0) break;
return makeUnexpected(DecodeVarintError::TooManyBytes);
} while (false);
} else {
......
......@@ -98,6 +98,17 @@ TEST(Varint, Simple) {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01});
}
void testVarintFail(std::initializer_list<uint8_t> bytes) {
size_t n = bytes.size();
ByteRange data(&*bytes.begin(), n);
auto ret = tryDecodeVarint(data);
EXPECT_FALSE(ret.hasValue());
}
TEST(Varint, Fail) {
testVarintFail({0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
}
TEST(ZigZag, Simple) {
EXPECT_EQ(0, encodeZigZag(0));
EXPECT_EQ(1, encodeZigZag(-1));
......
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