Skip to content

Commit da338aa

Browse files
committed
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.
1 parent 12dae63 commit da338aa

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/node/blockstorage.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,12 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const CBlockIndex& index) co
10881088
bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos) const
10891089
{
10901090
FlatFilePos hpos = pos;
1091+
// If nPos is less than 8 the pos is null and we don't have the block data
1092+
// Return early to prevent undefined behavior of unsigned int underflow
1093+
if (hpos.nPos < 8) {
1094+
LogError("%s: OpenBlockFile failed for %s\n", __func__, pos.ToString());
1095+
return false;
1096+
}
10911097
hpos.nPos -= 8; // Seek back 8 bytes for meta header
10921098
AutoFile filein{OpenBlockFile(hpos, true)};
10931099
if (filein.IsNull()) {

0 commit comments

Comments
 (0)