@@ -529,7 +529,7 @@ bool BlockManager::LoadBlockIndexDB(const std::optional<uint256>& snapshot_block
529
529
}
530
530
for (std::set<int >::iterator it = setBlkDataFiles.begin (); it != setBlkDataFiles.end (); it++) {
531
531
FlatFilePos pos (*it, 0 );
532
- if (OpenBlockFile (pos, true ).IsNull ()) {
532
+ if (OpenBlockFile (pos, /* fReadOnly= */ true ).IsNull ()) {
533
533
return false ;
534
534
}
535
535
}
@@ -933,7 +933,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
933
933
// Write undo information to disk
934
934
if (block.GetUndoPos ().IsNull ()) {
935
935
FlatFilePos pos;
936
- const unsigned int blockundo_size{static_cast <unsigned int >(GetSerializeSize (blockundo))};
936
+ const auto blockundo_size{static_cast <uint32_t >(GetSerializeSize (blockundo))};
937
937
if (!FindUndoPos (state, block.nFile , pos, blockundo_size + UNDO_DATA_DISK_OVERHEAD)) {
938
938
LogError (" FindUndoPos failed for %s while writing block undo" , pos.ToString ());
939
939
return false ;
@@ -996,7 +996,7 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
996
996
block.SetNull ();
997
997
998
998
// Open history file to read
999
- AutoFile filein{OpenBlockFile (pos, true )};
999
+ AutoFile filein{OpenBlockFile (pos, /* fReadOnly= */ true )};
1000
1000
if (filein.IsNull ()) {
1001
1001
LogError (" OpenBlockFile failed for %s while reading block" , pos.ToString ());
1002
1002
return false ;
@@ -1041,15 +1041,14 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
1041
1041
1042
1042
bool BlockManager::ReadRawBlock (std::vector<uint8_t >& block, const FlatFilePos& pos) const
1043
1043
{
1044
- FlatFilePos hpos = pos;
1045
- // If nPos is less than 8 the pos is null and we don't have the block data
1046
- // Return early to prevent undefined behavior of unsigned int underflow
1047
- if (hpos. nPos < 8 ) {
1048
- LogError (" Failed for %s while reading raw block" , pos.ToString ());
1044
+ if (pos. nPos < STORAGE_HEADER_BYTES) {
1045
+ // If nPos is less than STORAGE_HEADER_BYTES, we can't read the header that precedes the block data
1046
+ // This would cause an unsigned integer underflow when trying to position the file cursor
1047
+ // This can happen after pruning or default constructed positions
1048
+ LogError (" Failed for %s while reading raw block storage header " , pos.ToString ());
1049
1049
return false ;
1050
1050
}
1051
- hpos.nPos -= 8 ; // Seek back 8 bytes for meta header
1052
- AutoFile filein{OpenBlockFile (hpos, true )};
1051
+ AutoFile filein{OpenBlockFile ({pos.nFile , pos.nPos - STORAGE_HEADER_BYTES}, /* fReadOnly=*/ true )};
1053
1052
if (filein.IsNull ()) {
1054
1053
LogError (" OpenBlockFile failed for %s while reading raw block" , pos.ToString ());
1055
1054
return false ;
@@ -1091,7 +1090,7 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
1091
1090
LogError (" FindNextBlockPos failed for %s while writing block" , pos.ToString ());
1092
1091
return FlatFilePos ();
1093
1092
}
1094
- AutoFile fileout{OpenBlockFile (pos)};
1093
+ AutoFile fileout{OpenBlockFile (pos, /* fReadOnly= */ false )};
1095
1094
if (fileout.IsNull ()) {
1096
1095
LogError (" OpenBlockFile failed for %s while writing block" , pos.ToString ());
1097
1096
m_opts.notifications .fatalError (_ (" Failed to write block." ));
@@ -1210,7 +1209,7 @@ void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_
1210
1209
if (!fs::exists (chainman.m_blockman .GetBlockPosFilename (pos))) {
1211
1210
break ; // No block files left to reindex
1212
1211
}
1213
- AutoFile file{chainman.m_blockman .OpenBlockFile (pos, true )};
1212
+ AutoFile file{chainman.m_blockman .OpenBlockFile (pos, /* fReadOnly= */ true )};
1214
1213
if (file.IsNull ()) {
1215
1214
break ; // This error is logged in OpenBlockFile
1216
1215
}
0 commit comments