Add documentation for panics during size calculations
What changed, and why it matters
This commit only adds documentation comments explaining that certain size-calculation functions can panic if an arithmetic overflow occurs. It does not change any actual code behavior, fix a bug, or introduce new functionality. There is no security-relevant code change.
No action required; this is a documentation-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds # Panics doc comments to BlockCheckedExt::total_size, TransactionExt::base_size, TransactionExt::total_size, and Witness::size. The underlying overflow behavior already existed; this commit merely documents it. No logic, arithmetic, or error handling was modified.
Changed components
bitcoin/src/blockdata/block.rsbitcoin/src/blockdata/transaction.rsprimitives/src/witness.rsInspect captured patch +25 / −0
diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs
index 7558beef..70cb8220 100644
--- a/bitcoin/src/blockdata/block.rs
+++ b/bitcoin/src/blockdata/block.rs
@@ -273,6 +273,10 @@ pub trait BlockCheckedExt: sealed::Sealed {
///
/// > Total size is the block size in bytes with transactions serialized as described in BIP-0144,
/// > including base data and witness data.
+ ///
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
fn total_size(&self) -> usize;
/// Returns the coinbase transaction.
@@ -304,6 +308,9 @@ impl BlockCheckedExt for Block<Checked> {
Weight::from_wu(wu.to_u64())
}
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
fn total_size(&self) -> usize {
let mut size = Header::SIZE;
diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs
index 51210477..f22d8e29 100644
--- a/bitcoin/src/blockdata/transaction.rs
+++ b/bitcoin/src/blockdata/transaction.rs
@@ -286,12 +286,20 @@ pub trait TransactionExt: sealed::Sealed {
/// Returns the base transaction size.
///
/// > Base transaction size is the size of the transaction serialised with the witness data stripped.
+ ///
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
fn base_size(&self) -> usize;
/// Returns the total transaction size.
///
/// > Total transaction size is the transaction size in bytes serialized as described in BIP-0144,
/// > including base data and witness data.
+ ///
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
fn total_size(&self) -> usize;
/// Returns the "virtual size" (vsize) of this transaction.
@@ -385,6 +393,9 @@ impl TransactionExt for Transaction {
Weight::from_wu(wu.to_u64())
}
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
fn base_size(&self) -> usize {
let mut size: usize = 4; // Serialized length of a u32 for the version number.
@@ -397,6 +408,9 @@ impl TransactionExt for Transaction {
size + absolute::LockTime::SIZE
}
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
#[inline]
fn total_size(&self) -> usize {
let mut size: usize = 4; // Serialized length of a u32 for the version number.
diff --git a/primitives/src/witness.rs b/primitives/src/witness.rs
index 00446eb2..5a619ee0 100644
--- a/primitives/src/witness.rs
+++ b/primitives/src/witness.rs
@@ -123,6 +123,10 @@ impl Witness {
pub fn len(&self) -> usize { self.witness_elements }
/// Returns the number of bytes this witness contributes to a transactions total size.
+ ///
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
pub fn size(&self) -> usize {
let mut size: usize = 0;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.