diff options
author | Andrew Toth <andrewstoth@gmail.com> | 2024-03-12 12:46:07 -0400 |
---|---|---|
committer | Andrew Toth <andrewstoth@gmail.com> | 2024-03-12 12:46:07 -0400 |
commit | da338aada7943c392013c36c542af621fbc6edd1 (patch) | |
tree | 6e6a5fd2be78bb5f2dac5e4fd530ec9e4ac6b054 /src/node | |
parent | 12dae637a4723577d5404786879affc7ea981a7e (diff) | |
download | bitcoin-da338aada7943c392013c36c542af621fbc6edd1.tar.xz |
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.
Diffstat (limited to 'src/node')
-rw-r--r-- | src/node/blockstorage.cpp | 6 |
1 files changed, 6 insertions, 0 deletions
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<uint8_t>& 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()) { |