Skip to content

Commit 54e3174

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22455: addrman: detect on-disk corrupted nNew and nTried during unserialization
816f29e addrman: detect on-disk corrupted nNew and nTried during unserialization (Vasil Dimov) Pull request description: Negative `nNew` or `nTried` are not possible during normal operation. So, if we read such values during unserialize, report addrman corruption. Fixes bitcoin/bitcoin#22450 ACKs for top commit: MarcoFalke: cr ACK 816f29e jonatack: ACK 816f29e lsilva01: Code Review ACK bitcoin/bitcoin@816f29e. This change provides a more accurate description of the error. Tree-SHA512: 01bdd72d2d86a0ef770a319fee995fd1e147b24a8db84ddb8cd121688e7f94fed73fddc0084758e7183c4f8d08e971f0b1b224f5adb10928a5aa4dbbc8709d74
2 parents d3474b8 + 816f29e commit 54e3174

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/addrman.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,18 @@ class CAddrMan
334334
nUBuckets ^= (1 << 30);
335335
}
336336

337-
if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE) {
338-
throw std::ios_base::failure("Corrupt CAddrMan serialization, nNew exceeds limit.");
337+
if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nNew < 0) {
338+
throw std::ios_base::failure(
339+
strprintf("Corrupt CAddrMan serialization: nNew=%d, should be in [0, %u]",
340+
nNew,
341+
ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE));
339342
}
340343

341-
if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE) {
342-
throw std::ios_base::failure("Corrupt CAddrMan serialization, nTried exceeds limit.");
344+
if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nTried < 0) {
345+
throw std::ios_base::failure(
346+
strprintf("Corrupt CAddrMan serialization: nTried=%d, should be in [0, %u]",
347+
nTried,
348+
ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE));
343349
}
344350

345351
// Deserialize entries from the new table.

0 commit comments

Comments
 (0)