Skip to content

Commit

Permalink
Prevent integer overflow in ReadVarInt.
Browse files Browse the repository at this point in the history
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.

Cherry-picked from: 45f0961
  • Loading branch information
gmaxwell authored and xanimo committed Jul 6, 2024
1 parent 962b020 commit a2d4a4e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,18 @@ I ReadVarInt(Stream& is)
I n = 0;
while(true) {
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);
if (chData & 0x80)
if (chData & 0x80) {
if (n == std::numeric_limits<I>::max()) {
throw std::ios_base::failure("ReadVarInt(): size too large");
}
n++;
else
} else {
return n;
}
}
}

Expand Down

0 comments on commit a2d4a4e

Please sign in to comment.