Skip to content

Commit 45f0961

Browse files
committed
Prevent integer overflow in ReadVarInt.
We don't normally use ReadVarInt from untrusted inputs, but we might see this in the case of corruption. This is exposed in test_bitcoin_fuzzy.
1 parent 923dc44 commit 45f0961

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/serialize.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,18 @@ I ReadVarInt(Stream& is)
336336
I n = 0;
337337
while(true) {
338338
unsigned char chData = ser_readdata8(is);
339+
if (n > (std::numeric_limits<I>::max() >> 7)) {
340+
throw std::ios_base::failure("ReadVarInt(): size too large");
341+
}
339342
n = (n << 7) | (chData & 0x7F);
340-
if (chData & 0x80)
343+
if (chData & 0x80) {
344+
if (n == std::numeric_limits<I>::max()) {
345+
throw std::ios_base::failure("ReadVarInt(): size too large");
346+
}
341347
n++;
342-
else
348+
} else {
343349
return n;
350+
}
344351
}
345352
}
346353

0 commit comments

Comments
 (0)