@@ -529,7 +529,7 @@ bool BlockManager::LoadBlockIndexDB(const std::optional<uint256>& snapshot_block
529529 }
530530 for (std::set<int >::iterator it = setBlkDataFiles.begin (); it != setBlkDataFiles.end (); it++) {
531531 FlatFilePos pos (*it, 0 );
532- if (OpenBlockFile (pos, true ).IsNull ()) {
532+ if (OpenBlockFile (pos, /* fReadOnly= */ true ).IsNull ()) {
533533 return false ;
534534 }
535535 }
@@ -933,7 +933,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
933933 // Write undo information to disk
934934 if (block.GetUndoPos ().IsNull ()) {
935935 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))};
937937 if (!FindUndoPos (state, block.nFile , pos, blockundo_size + UNDO_DATA_DISK_OVERHEAD)) {
938938 LogError (" FindUndoPos failed for %s while writing block undo" , pos.ToString ());
939939 return false ;
@@ -996,7 +996,7 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
996996 block.SetNull ();
997997
998998 // Open history file to read
999- AutoFile filein{OpenBlockFile (pos, true )};
999+ AutoFile filein{OpenBlockFile (pos, /* fReadOnly= */ true )};
10001000 if (filein.IsNull ()) {
10011001 LogError (" OpenBlockFile failed for %s while reading block" , pos.ToString ());
10021002 return false ;
@@ -1041,15 +1041,14 @@ bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
10411041
10421042bool BlockManager::ReadRawBlock (std::vector<uint8_t >& block, const FlatFilePos& pos) const
10431043{
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 ());
10491049 return false ;
10501050 }
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 )};
10531052 if (filein.IsNull ()) {
10541053 LogError (" OpenBlockFile failed for %s while reading raw block" , pos.ToString ());
10551054 return false ;
@@ -1091,7 +1090,7 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
10911090 LogError (" FindNextBlockPos failed for %s while writing block" , pos.ToString ());
10921091 return FlatFilePos ();
10931092 }
1094- AutoFile fileout{OpenBlockFile (pos)};
1093+ AutoFile fileout{OpenBlockFile (pos, /* fReadOnly= */ false )};
10951094 if (fileout.IsNull ()) {
10961095 LogError (" OpenBlockFile failed for %s while writing block" , pos.ToString ());
10971096 m_opts.notifications .fatalError (_ (" Failed to write block." ));
@@ -1210,7 +1209,7 @@ void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_
12101209 if (!fs::exists (chainman.m_blockman .GetBlockPosFilename (pos))) {
12111210 break ; // No block files left to reindex
12121211 }
1213- AutoFile file{chainman.m_blockman .OpenBlockFile (pos, true )};
1212+ AutoFile file{chainman.m_blockman .OpenBlockFile (pos, /* fReadOnly= */ true )};
12141213 if (file.IsNull ()) {
12151214 break ; // This error is logged in OpenBlockFile
12161215 }
0 commit comments