Skip to content

Commit fa7a883

Browse files
author
MarcoFalke
committed
addrman: Replace assert with throw on corrupt data
Assert should only be used for program internal logic errors, not to sanitize external user input.
1 parent fa29897 commit fa7a883

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/addrman.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,12 @@ void CAddrMan::Unserialize(Stream& s_)
386386
LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions or invalid addresses\n", nLostUnk, nLost);
387387
}
388388

389-
Check();
389+
const int check_code{ForceCheckAddrman()};
390+
if (check_code != 0) {
391+
throw std::ios_base::failure(strprintf(
392+
"Corrupt data. Consistency check failed with code %s",
393+
check_code));
394+
}
390395
}
391396

392397
// explicit instantiation

test/functional/feature_addrman.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def serialize_addrman(
1919
format=1,
2020
lowest_compatible=3,
2121
net_magic="regtest",
22+
bucket_key=1,
2223
len_new=None,
2324
len_tried=None,
2425
mock_checksum=None,
@@ -29,7 +30,7 @@ def serialize_addrman(
2930
r = MAGIC_BYTES[net_magic]
3031
r += struct.pack("B", format)
3132
r += struct.pack("B", INCOMPATIBILITY_BASE + lowest_compatible)
32-
r += ser_uint256(1)
33+
r += ser_uint256(bucket_key)
3334
r += struct.pack("i", len_new or len(new))
3435
r += struct.pack("i", len_tried or len(tried))
3536
ADDRMAN_NEW_BUCKET_COUNT = 1 << 10
@@ -119,6 +120,14 @@ def run_test(self):
119120
match=ErrorMatch.FULL_REGEX,
120121
)
121122

123+
self.log.info("Check that corrupt addrman cannot be read (failed check)")
124+
self.stop_node(0)
125+
write_addrman(peers_dat, bucket_key=0)
126+
self.nodes[0].assert_start_raises_init_error(
127+
expected_msg=init_error("Corrupt data. Consistency check failed with code -16: .*"),
128+
match=ErrorMatch.FULL_REGEX,
129+
)
130+
122131
self.log.info("Check that missing addrman is recreated")
123132
self.stop_node(0)
124133
os.remove(peers_dat)

0 commit comments

Comments
 (0)