From 499d95e278f34790660a2b9baf5525e0def1485a Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Mon, 13 Feb 2017 13:41:02 -0500 Subject: Add static_assert to prevent VARINT() Using VARINT with signed types is dangerous because negative values will appear to serialize correctly, but then deserialize as positive values mod 128. This commit changes the VARINT macro to trigger an error by default if called with an signed value, and updates broken uses of VARINT to pass a special flag that lets them keep working with no change in behavior. --- src/serialize.h | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) (limited to 'src/serialize.h') diff --git a/src/serialize.h b/src/serialize.h index c454ba16b7..91da6b0f80 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -296,9 +296,31 @@ uint64_t ReadCompactSize(Stream& is) * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00] */ -template +/** + * Mode for encoding VarInts. + * + * Currently there is no support for signed encodings. The default mode will not + * compile with signed values, and the legacy "nonnegative signed" mode will + * accept signed values, but improperly encode and decode them if they are + * negative. In the future, the DEFAULT mode could be extended to support + * negative numbers in a backwards compatible way, and additional modes could be + * added to support different varint formats (e.g. zigzag encoding). + */ +enum class VarIntMode { DEFAULT, NONNEGATIVE_SIGNED }; + +template +struct CheckVarIntMode { + constexpr CheckVarIntMode() + { + static_assert(Mode != VarIntMode::DEFAULT || std::is_unsigned::value, "Unsigned type required with mode DEFAULT."); + static_assert(Mode != VarIntMode::NONNEGATIVE_SIGNED || std::is_signed::value, "Signed type required with mode NONNEGATIVE_SIGNED."); + } +}; + +template inline unsigned int GetSizeOfVarInt(I n) { + CheckVarIntMode(); int nRet = 0; while(true) { nRet++; @@ -312,9 +334,10 @@ inline unsigned int GetSizeOfVarInt(I n) template inline void WriteVarInt(CSizeComputer& os, I n); -template +template void WriteVarInt(Stream& os, I n) { + CheckVarIntMode(); unsigned char tmp[(sizeof(n)*8+6)/7]; int len=0; while(true) { @@ -329,9 +352,10 @@ void WriteVarInt(Stream& os, I n) } while(len--); } -template +template I ReadVarInt(Stream& is) { + CheckVarIntMode(); I n = 0; while(true) { unsigned char chData = ser_readdata8(is); @@ -351,7 +375,7 @@ I ReadVarInt(Stream& is) } #define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)) -#define VARINT(obj) WrapVarInt(REF(obj)) +#define VARINT(obj, ...) WrapVarInt<__VA_ARGS__>(REF(obj)) #define COMPACTSIZE(obj) CCompactSize(REF(obj)) #define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj)) @@ -395,7 +419,7 @@ public: } }; -template +template class CVarInt { protected: @@ -405,12 +429,12 @@ public: template void Serialize(Stream &s) const { - WriteVarInt(s, n); + WriteVarInt(s, n); } template void Unserialize(Stream& s) { - n = ReadVarInt(s); + n = ReadVarInt(s); } }; @@ -461,8 +485,8 @@ public: } }; -template -CVarInt WrapVarInt(I& n) { return CVarInt(n); } +template +CVarInt WrapVarInt(I& n) { return CVarInt{n}; } /** * Forward declarations -- cgit v1.2.3