Skip to content

Commit 8153bd9

Browse files
jamesobryanofsky
andcommitted
blockmanager: avoid undefined behavior during FlushBlockFile
If we call FlushBlockFile() without having intitialized the block index with LoadBlockIndexDB(), we may be indexing into an empty vector. Specifically this is an issue when we call MaybeRebalanceCaches() during chainstate init before the block index has been loaded, which calls FlushBlockFile(). Also add an assert to avoid undefined behavior. Co-authored-by: Russell Yanofsky <[email protected]>
1 parent ad67ff3 commit 8153bd9

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/node/blockstorage.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,16 @@ void BlockManager::FlushUndoFile(int block_file, bool finalize)
524524
void BlockManager::FlushBlockFile(bool fFinalize, bool finalize_undo)
525525
{
526526
LOCK(cs_LastBlockFile);
527+
528+
if (m_blockfile_info.size() < 1) {
529+
// Return if we haven't loaded any blockfiles yet. This happens during
530+
// chainstate init, when we call ChainstateManager::MaybeRebalanceCaches() (which
531+
// then calls FlushStateToDisk()), resulting in a call to this function before we
532+
// have populated `m_blockfile_info` via LoadBlockIndexDB().
533+
return;
534+
}
535+
assert(static_cast<int>(m_blockfile_info.size()) > m_last_blockfile);
536+
527537
FlatFilePos block_pos_old(m_last_blockfile, m_blockfile_info[m_last_blockfile].nSize);
528538
if (!BlockFileSeq().Flush(block_pos_old, fFinalize)) {
529539
AbortNode("Flushing block file to disk failed. This is likely the result of an I/O error.");

0 commit comments

Comments
 (0)