Merge #9693: Prevent integer overflow in ReadVarInt.

45f0961 Prevent integer overflow in ReadVarInt. (Gregory Maxwell)

Tree-SHA512: 385ea0efb6b59d44c45a49227e5f6fff236b4775544cbeb236312a3fd87fd75c226ac56f7aa1bca66b853639da75a579610074f7582f92cf2ebd4a74bc40f6f0
This commit is contained in:
Pieter Wuille 2017-04-17 04:46:34 -07:00 committed by Pasta
parent 0db1328a24
commit 632956a806
No known key found for this signature in database
GPG Key ID: 0B8EB7A31A44D9C6

View File

@ -352,13 +352,20 @@ I ReadVarInt(Stream& is)
I n = 0; I n = 0;
while(true) { while(true) {
unsigned char chData = ser_readdata8(is); unsigned char chData = ser_readdata8(is);
if (n > (std::numeric_limits<I>::max() >> 7)) {
throw std::ios_base::failure("ReadVarInt(): size too large");
}
n = (n << 7) | (chData & 0x7F); n = (n << 7) | (chData & 0x7F);
if (chData & 0x80) if (chData & 0x80) {
if (n == std::numeric_limits<I>::max()) {
throw std::ios_base::failure("ReadVarInt(): size too large");
}
n++; n++;
else } else {
return n; return n;
} }
} }
}
#define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))) #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj)))
#define FIXEDBITSET(obj, size) REF(CFixedBitSet(REF(obj), (size))) #define FIXEDBITSET(obj, size) REF(CFixedBitSet(REF(obj), (size)))