From ba9034b9647da166a87ef42583415fbcc8127255 Mon Sep 17 00:00:00 2001 From: Jon Maltiel Swenson Date: Wed, 31 May 2017 08:20:51 -0700 Subject: [PATCH] Add non-throwing alternative to decodeVarint Summary: In mcrouter code, we would like a non-throwing alternative to `decodeVarint()`. There are real, expected scenarios where only part of a serialized varint fits into a packet, in which case mcrouter fails to decode the varint and throws. In these scenarios, throwing too many exceptions can lead to lock contention and degrade performance. Reviewed By: yfeldblum Differential Revision: D5153823 fbshipit-source-id: 138273af832903f0b04bee0bcacddd66b4274129 --- folly/Varint.h | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/folly/Varint.h b/folly/Varint.h index 4664cb95..84e275ad 100644 --- a/folly/Varint.h +++ b/folly/Varint.h @@ -17,7 +17,10 @@ #pragma once #include + #include +#include +#include #include namespace folly { @@ -55,10 +58,24 @@ size_t encodeVarint(uint64_t val, uint8_t* buf); /** * Decode a value from a given buffer, advances data past the returned value. + * Throws on error. */ template uint64_t decodeVarint(Range& data); +enum class DecodeVarintError { + TooManyBytes = 0, + TooFewBytes = 1, +}; + +/** + * A variant of decodeVarint() that does not throw on error. Useful in contexts + * where only part of a serialized varint may be attempted to be decoded, e.g., + * when a serialized varint arrives on the boundary of a network packet. + */ +template +Expected tryDecodeVarint(Range& data); + /** * ZigZag encoding that maps signed integers with a small absolute value * to unsigned integers with a small (positive) values. Without this, @@ -93,6 +110,18 @@ inline size_t encodeVarint(uint64_t val, uint8_t* buf) { template inline uint64_t decodeVarint(Range& data) { + auto expected = tryDecodeVarint(data); + if (!expected) { + throw std::invalid_argument( + expected.error() == DecodeVarintError::TooManyBytes + ? "Invalid varint value: too many bytes." + : "Invalid varint value: too few bytes."); + } + return *expected; +} + +template +inline Expected tryDecodeVarint(Range& data) { static_assert( std::is_same::type, char>::value || std::is_same::type, unsigned char>::value, @@ -117,7 +146,7 @@ inline uint64_t decodeVarint(Range& data) { 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; - throw std::invalid_argument("Invalid varint value. Too big."); + return makeUnexpected(DecodeVarintError::TooManyBytes); } while (false); } else { int shift = 0; @@ -126,9 +155,7 @@ inline uint64_t decodeVarint(Range& data) { shift += 7; } if (p == end) { - throw std::invalid_argument("Invalid varint value. Too small: " + - folly::to(end - begin) + - " bytes"); + return makeUnexpected(DecodeVarintError::TooFewBytes); } val |= static_cast(*p++) << shift; } @@ -137,4 +164,4 @@ inline uint64_t decodeVarint(Range& data) { return val; } -} // namespaces +} // folly -- 2.34.1