Update `Weight` and `TxInExt` functions with panic details
What changed, and why it matters
This commit only adds documentation comments describing situations where certain functions could panic due to arithmetic overflow. No code behavior was changed, so this is a documentation-only update with no security impact.
No action required; this is a documentation-only change. Reviewers may optionally verify the documented panic conditions match actual implementation behavior.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds ‘# Panics’ doc comments to Weight construction helpers (from_non_witness_data_size, from_witness_data_size implicitly) and TxInExt methods (legacy_weight, segwit_weight, base_size, total_size). The underlying arithmetic and overflow behavior remain unchanged; only the public API documentation is updated to warn callers about overflow panics.
Changed components
bitcoin/src/blockdata/transaction.rsunits/src/weight.rsInspect captured patch +20 / −0
diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs
index a8c045fa..9b79cbf5 100644
--- a/bitcoin/src/blockdata/transaction.rs
+++ b/bitcoin/src/blockdata/transaction.rs
@@ -134,6 +134,10 @@ internal_macros::define_extension_trait! {
/// Keep in mind that when adding a TxIn to a transaction, the total weight of the transaction
/// might increase more than `TxIn::legacy_weight`. This happens when the new input added causes
/// the input length `CompactSize` to increase its encoding length.
+ ///
+ /// # Panics
+ ///
+ /// If the coversion overflows.
fn legacy_weight(&self) -> Weight {
Weight::from_non_witness_data_size(self.base_size().to_u64())
}
@@ -149,6 +153,10 @@ internal_macros::define_extension_trait! {
/// - the new input added causes the input length `CompactSize` to increase its encoding length
/// - the new input is the first segwit input added - this will add an additional 2WU to the
/// transaction weight to take into account the SegWit marker
+ ///
+ /// # Panics
+ ///
+ /// If the coversion overflows.
fn segwit_weight(&self) -> Weight {
Weight::from_non_witness_data_size(self.base_size().to_u64())
+ Weight::from_witness_data_size(self.witness.size().to_u64())
@@ -157,6 +165,10 @@ internal_macros::define_extension_trait! {
/// Returns the base size of this input.
///
/// Base size excludes the witness data (see [`Self::total_size`]).
+ ///
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
fn base_size(&self) -> usize {
let mut size = OutPoint::SIZE;
@@ -169,6 +181,10 @@ internal_macros::define_extension_trait! {
/// Returns the total number of bytes that this input contributes to a transaction.
///
/// Total size includes the witness data (for base size see [`Self::base_size`]).
+ ///
+ /// # Panics
+ ///
+ /// If the size calculation overflows.
fn total_size(&self) -> usize { self.base_size() + self.witness.size() }
}
}
diff --git a/units/src/weight.rs b/units/src/weight.rs
index 1ef38343..3d8d257d 100644
--- a/units/src/weight.rs
+++ b/units/src/weight.rs
@@ -99,6 +99,10 @@ impl Weight {
pub const fn from_witness_data_size(witness_size: u64) -> Self { Weight::from_wu(witness_size) }
/// Constructs a new [`Weight`] from non-witness size.
+ ///
+ /// # Panics
+ ///
+ /// If the conversion from virtual bytes overflows.
pub const fn from_non_witness_data_size(non_witness_size: u64) -> Self {
Weight::from_wu(non_witness_size * Self::WITNESS_SCALE_FACTOR)
}
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.