Skip to content

Commit b07fdd7

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24312: addrman: Log too low compat value
fa097d0 addrman: Log too low compat value (MarcoFalke) Pull request description: Before this patch, when writing a negative `lowest_compatible` value, it would be read as a positive value. For example `-32` will be read as `224`. There is generally nothing wrong with that. Though, similarly there shouldn't be anything wrong with refusing to read a negative value. I find the code after this patch more logical than before. Also, this allows dropping a file-wide sanitizer suppression. In practice none of this should ever happen. Bitcoin Core would never write a negative `lowest_compatible` in normal operation, unless the file storage is later corrupted by external influence. ACKs for top commit: mzumsande: re-ACK fa097d0 Tree-SHA512: 9aae7b8fe666f52f667f149667025e0160cef1a793cc4d392e36608f65c2bee8096da429235118f40a3368f327aabe30f3732ae78c5874648ea6f423f2687b65
2 parents 64a4483 + fa097d0 commit b07fdd7

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/addrman.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,18 @@ void AddrManImpl::Unserialize(Stream& s_)
246246

247247
uint8_t compat;
248248
s >> compat;
249+
if (compat < INCOMPATIBILITY_BASE) {
250+
throw std::ios_base::failure(strprintf(
251+
"Corrupted addrman database: The compat value (%u) "
252+
"is lower than the expected minimum value %u.",
253+
compat, INCOMPATIBILITY_BASE));
254+
}
249255
const uint8_t lowest_compatible = compat - INCOMPATIBILITY_BASE;
250256
if (lowest_compatible > FILE_FORMAT) {
251257
throw InvalidAddrManVersionError(strprintf(
252258
"Unsupported format of addrman database: %u. It is compatible with formats >=%u, "
253259
"but the maximum supported by this version of %s is %u.",
254-
uint8_t{format}, uint8_t{lowest_compatible}, PACKAGE_NAME, uint8_t{FILE_FORMAT}));
260+
uint8_t{format}, lowest_compatible, PACKAGE_NAME, uint8_t{FILE_FORMAT}));
255261
}
256262

257263
s >> nKey;

test/functional/feature_addrman.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ def run_test(self):
6868
self.start_node(0, extra_args=["-checkaddrman=1"])
6969
assert_equal(self.nodes[0].getnodeaddresses(), [])
7070

71+
self.log.info("Check that addrman with negative lowest_compatible cannot be read")
72+
self.stop_node(0)
73+
write_addrman(peers_dat, lowest_compatible=-32)
74+
self.nodes[0].assert_start_raises_init_error(
75+
expected_msg=init_error(
76+
"Corrupted addrman database: The compat value \\(0\\) is lower "
77+
"than the expected minimum value 32.: (.+)"
78+
),
79+
match=ErrorMatch.FULL_REGEX,
80+
)
81+
7182
self.log.info("Check that addrman from future is overwritten with new addrman")
7283
self.stop_node(0)
7384
write_addrman(peers_dat, lowest_compatible=111)

test/sanitizer_suppressions/ubsan

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ implicit-integer-sign-change:script/bitcoinconsensus.cpp
6262
implicit-integer-sign-change:script/interpreter.cpp
6363
implicit-integer-sign-change:serialize.h
6464
implicit-integer-sign-change:txmempool.cpp
65-
implicit-signed-integer-truncation:addrman.cpp
6665
implicit-signed-integer-truncation:crypto/
6766
implicit-unsigned-integer-truncation:crypto/
6867
shift-base:arith_uint256.cpp

0 commit comments

Comments
 (0)