Skip to content

Commit f50fb17

Browse files
committed
Merge bitcoin/bitcoin#27235: Avoid integer overflow in CheckDiskSpace
05eeba2 [test] Add manual prune startup test case (dergoegge) 4517419 [util] Avoid integer overflow in CheckDiskSpace (dergoegge) Pull request description: Starting a fresh node with `-prune=1` causes an integer overflow to happen in `CheckDiskSpace` ([here](https://github.com/bitcoin/bitcoin/blob/f7bdcfc83f5753349018be3b5a663c8923d1a5eb/src/init.cpp#L1633-L1648)) because `nPruneTarget` is to the max `uint64_t` value. ``` node1 stderr util/system.cpp:138:51: runtime error: unsigned integer overflow: 52428800 + 18446744073709551615 cannot be represented in type 'unsigned long' #0 0x564a482b5088 in CheckDiskSpace(fs::path const&, unsigned long) src/./src/util/system.cpp:138:51 #1 0x564a4728dc59 in AppInitMain(node::NodeContext&, interfaces::BlockAndHeaderTipInfo*) src/./src/init.cpp:1639:14 #2 0x564a47256e6a in AppInit(node::NodeContext&, int, char**) src/./src/bitcoind.cpp:221:43 #3 0x564a47256087 in main src/./src/bitcoind.cpp:265:13 bitcoin-core#4 0x7fcb7cbffd8f (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: 69389d485a9793dbe873f0ea2c93e02efaa9aa3d) bitcoin-core#5 0x7fcb7cbffe3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId: 69389d485a9793dbe873f0ea2c93e02efaa9aa3d) bitcoin-core#6 0x564a471957f4 in _start (/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/src/bitcoind+0xca07f4) (BuildId: 035cb22302d37317a630900a15a26ecb326d395c) SUMMARY: UndefinedBehaviorSanitizer: unsigned-integer-overflow util/system.cpp:138:51 in ``` I think side stepping the overflow for this specific case, is better than adding an exception to the UB suppresions file. ACKs for top commit: MarcoFalke: ACK 05eeba2 🥝 john-moffett: ACK 05eeba2 Tree-SHA512: 1d8e6bcb49818139f04b5ab2cbef7f9b422bf0c38a804cd532b6bd0ba4c4fd07f959ba977e59896343f213086c8ecc48180f50d006638dc84649c66ec379d58a
2 parents 73a9892 + 05eeba2 commit f50fb17

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/init.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1631,10 +1631,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16311631

16321632
// On first startup, warn on low block storage space
16331633
if (!fReindex && !fReindexChainState && chain_active_height <= 1) {
1634+
uint64_t assumed_chain_bytes{chainparams.AssumedBlockchainSize() * 1024 * 1024 * 1024};
16341635
uint64_t additional_bytes_needed{
16351636
chainman.m_blockman.IsPruneMode() ?
1636-
chainman.m_blockman.GetPruneTarget() :
1637-
chainparams.AssumedBlockchainSize() * 1024 * 1024 * 1024};
1637+
std::min(chainman.m_blockman.GetPruneTarget(), assumed_chain_bytes) :
1638+
assumed_chain_bytes};
16381639

16391640
if (!CheckDiskSpace(args.GetBlocksDirPath(), additional_bytes_needed)) {
16401641
InitWarning(strprintf(_(

test/functional/rpc_blockchain.py

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def set_test_params(self):
6969

7070
def run_test(self):
7171
self.wallet = MiniWallet(self.nodes[0])
72+
self._test_prune_disk_space()
7273
self.mine_chain()
7374
self._test_max_future_block_time()
7475
self.restart_node(
@@ -100,6 +101,13 @@ def mine_chain(self):
100101
self.generate(self.wallet, 1)
101102
assert_equal(self.nodes[0].getblockchaininfo()['blocks'], HEIGHT)
102103

104+
def _test_prune_disk_space(self):
105+
self.log.info("Test that a manually pruned node does not run into "
106+
"integer overflow on first start up")
107+
self.restart_node(0, extra_args=["-prune=1"])
108+
self.log.info("Avoid warning when assumed chain size is enough")
109+
self.restart_node(0, extra_args=["-prune=123456789"])
110+
103111
def _test_max_future_block_time(self):
104112
self.stop_node(0)
105113
self.log.info("A block tip of more than MAX_FUTURE_BLOCK_TIME in the future raises an error")

0 commit comments

Comments
 (0)