From da338aada7943c392013c36c542af621fbc6edd1 Mon Sep 17 00:00:00 2001 From: Andrew Toth Date: Tue, 12 Mar 2024 12:46:07 -0400 Subject: blockstorage: check nPos in ReadRawBlockFromDisk before seeking back ReadRawBlockFromDisk assumes a non-null pos that has an nPos >= 8. This simple check makes the function safer to call in the future, so callers don't need to worry about causing UB if the pos is null. --- src/node/blockstorage.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/node') diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 211d557826..d0ddfeb0f1 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -1088,6 +1088,12 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const CBlockIndex& index) co bool BlockManager::ReadRawBlockFromDisk(std::vector& block, const FlatFilePos& pos) const { FlatFilePos hpos = pos; + // If nPos is less than 8 the pos is null and we don't have the block data + // Return early to prevent undefined behavior of unsigned int underflow + if (hpos.nPos < 8) { + LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString()); + return false; + } hpos.nPos -= 8; // Seek back 8 bytes for meta header AutoFile filein{OpenBlockFile(hpos, true)}; if (filein.IsNull()) { -- cgit v1.2.3