Skip to content

Commit

Permalink
Merge bitcoin#24312: addrman: Log too low compat value
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MarcoFalke authored and PastaPastaPasta committed Nov 17, 2024
1 parent d96983a commit 3383b79
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,18 @@ void AddrManImpl::Unserialize(Stream& s_)

uint8_t compat;
s >> compat;
if (compat < INCOMPATIBILITY_BASE) {
throw std::ios_base::failure(strprintf(
"Corrupted addrman database: The compat value (%u) "
"is lower than the expected minimum value %u.",
compat, INCOMPATIBILITY_BASE));
}
const uint8_t lowest_compatible = compat - INCOMPATIBILITY_BASE;
if (lowest_compatible > FILE_FORMAT) {
throw InvalidAddrManVersionError(strprintf(
"Unsupported format of addrman database: %u. It is compatible with formats >=%u, "
"but the maximum supported by this version of %s is %u.",
uint8_t{format}, uint8_t{lowest_compatible}, PACKAGE_NAME, uint8_t{FILE_FORMAT}));
uint8_t{format}, lowest_compatible, PACKAGE_NAME, uint8_t{FILE_FORMAT}));
}

s >> nKey;
Expand Down
11 changes: 11 additions & 0 deletions test/functional/feature_addrman.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def run_test(self):
self.start_node(0, extra_args=["-checkaddrman=1"])
assert_equal(self.nodes[0].getnodeaddresses(), [])

self.log.info("Check that addrman with negative lowest_compatible cannot be read")
self.stop_node(0)
write_addrman(peers_dat, lowest_compatible=-32)
self.nodes[0].assert_start_raises_init_error(
expected_msg=init_error(
"Corrupted addrman database: The compat value \\(0\\) is lower "
"than the expected minimum value 32.: (.+)"
),
match=ErrorMatch.FULL_REGEX,
)

self.log.info("Check that addrman from future is overwritten with new addrman")
self.stop_node(0)
write_addrman(peers_dat, lowest_compatible=111)
Expand Down
1 change: 0 additions & 1 deletion test/sanitizer_suppressions/ubsan
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ implicit-integer-sign-change:util/strencodings.h
implicit-integer-sign-change:validation.cpp
implicit-signed-integer-truncation,implicit-integer-sign-change:chain.h
implicit-signed-integer-truncation,implicit-integer-sign-change:test/skiplist_tests.cpp
implicit-signed-integer-truncation:addrman.cpp
implicit-signed-integer-truncation:addrman.h
implicit-signed-integer-truncation:chain.h
implicit-signed-integer-truncation:crypto/
Expand Down

0 comments on commit 3383b79

Please sign in to comment.