Assert should only be used for program internal logic errors, not to sanitize external user input.
The assert was introduced via the debug-only runtime option -checkaddrman
in commit 803ef70fd9f65ef800567ff9456fac525bc3e3c2, thus won’t need a backport.
Also, it doesn’t really make sense to continue when the deserialized addrman doesn’t pass the sanity check.
For example, if nLastSuccess
is negative, it would later result in integer overflows. Thus, this patch fixes #22931.
Also, Fixes #22503 Fixes #22504 Fixes #22519
Closes #22498
Steps to test:
0mkdir -p /tmp/test_235/regtest/
1echo 'H4sIAAAAAAAAA/u1f+stZmUGYgELgwPRakfBKBgFo2AUjIJRMApGwSgYBaNgFIyCUTBswdyGpFnLjUKjP9e0bvjYusl6b+L2e7Vs2dd6N//Pua0/xQUALJAn93IQAAA=' | base64 --decode | zcat > /tmp/test_235/regtest/peers.dat
2./src/qt/bitcoin-qt -regtest -datadir=/tmp/test_235/ -checkaddrman=1 -printtoconsole | grep -A2 'Loading P2P addresses'
Output before:
02021-09-10T11:28:37Z init message: Loading P2P addresses…
12021-09-10T11:28:37Z ADDRMAN CONSISTENCY CHECK FAILED!!! err=-16
2bitcoin-qt: addrman.cpp:765: void CAddrMan::Check() const: Assertion `false' failed.
3
4(program crashes)
Output after:
02021-09-10T11:26:00Z init message: Loading P2P addresses…
12021-09-10T11:26:00Z Error: Invalid or corrupt peers.dat (Corrupt data. Consistency check failed with code -16: iostream error). If you believe this is a bug, please report it to https://github.com/bitcoin/bitcoin/issues. As a workaround, you can move the file ("/tmp/test_235/regtest/peers.dat") out of the way (rename, move, or delete) to have a new one created on the next start.
2
3(program exits)