fix: prevents potential arithmetic underflow (#8156)

This commit is contained in:
Federico Gimenez
2024-05-08 15:21:16 +02:00
committed by GitHub
parent 04d2c10c46
commit 72e5122e73
2 changed files with 7 additions and 2 deletions

View File

@ -119,7 +119,8 @@ impl AppendableChain {
DB: Database + Clone,
E: BlockExecutorProvider,
{
let parent_number = block.number - 1;
let parent_number =
block.number.checked_sub(1).ok_or(BlockchainTreeError::GenesisBlockHasNoParent)?;
let parent = self.blocks().get(&parent_number).ok_or(
BlockchainTreeError::BlockNumberNotFoundInChain { block_number: parent_number },
)?;

View File

@ -47,6 +47,9 @@ pub enum BlockchainTreeError {
/// The block hash of the block that failed to buffer.
block_hash: BlockHash,
},
/// Thrown when trying to access genesis parent.
#[error("genesis block has no parent")]
GenesisBlockHasNoParent,
}
/// Canonical Errors
@ -318,7 +321,8 @@ impl InsertBlockErrorKind {
BlockchainTreeError::CanonicalChain { .. } |
BlockchainTreeError::BlockNumberNotFoundInChain { .. } |
BlockchainTreeError::BlockHashNotFoundInChain { .. } |
BlockchainTreeError::BlockBufferingFailed { .. } => false,
BlockchainTreeError::BlockBufferingFailed { .. } |
BlockchainTreeError::GenesisBlockHasNoParent => false,
}
}
InsertBlockErrorKind::Provider(_) | InsertBlockErrorKind::Internal(_) => {