Merge rust-bitcoin/rust-bitcoin#7007: primitives: Add track_caller to panicking functions
What changed, and why it matters
This change is a code-quality and documentation improvement, not a security fix. It adds Rust's #[track_caller] attribute to several functions that can panic when given extremely large inputs, and documents those panic conditions. The panics themselves already existed; the patch only makes error messages more accurate and documents behavior. There is no vulnerability being fixed.
No security action required. Treat as normal maintenance/documentation improvement.
Security signals we found
No security signal: change is purely documentation and panic-location metadata
Functions already panicked; no new panic paths introduced
No bounds checks added or removed
No unsafe code modified
Evidence from the diff
The commit adds #[track_caller] to ScriptBuf::with_capacity, reserve, reserve_exact, and to Witness::from_slice, push, push_slice, and encode_cursor. It also adds Panics documentation for the Witness constructors and ScriptBuf::with_capacity, and removes two stale duplicate-code comments. The underlying panic conditions (Vec capacity exceeding isize::MAX, Witness offsets exceeding u32::MAX) are pre-existing behavior of the standard library and the existing Witness implementation. The patch does not alter any logic, bounds checks, or arithmetic; it only affects panic location reporting and documentation.
Changed components
primitives/src/script/owned.rsprimitives/src/transaction.rsprimitives/src/witness.rsInspect captured patch +21 / −2
### primitives/src/script/owned.rs
@@ -119,7 +119,12 @@ impl<T> ScriptBuf<T> {
}
/// Constructs a new empty script with at least the specified capacity.
+ ///
+ /// # Panics
+ ///
+ /// Panics if the new capacity exceeds `isize::MAX` bytes.
#[inline]
+ #[track_caller]
pub fn with_capacity(capacity: usize) -> Self { Self::from_bytes(Vec::with_capacity(capacity)) }
/// Pre-allocates at least `additional_len` bytes if needed.
@@ -133,6 +138,7 @@ impl<T> ScriptBuf<T> {
///
/// Panics if the new capacity exceeds `isize::MAX` bytes.
#[inline]
+ #[track_caller]
pub fn reserve(&mut self, additional_len: usize) { self.1.reserve(additional_len); }
/// Pre-allocates exactly `additional_len` bytes if needed.
@@ -151,6 +157,7 @@ impl<T> ScriptBuf<T> {
///
/// [`reserve`]: Self::reserve
#[inline]
+ #[track_caller]
pub fn reserve_exact(&mut self, additional_len: usize) { self.1.reserve_exact(additional_len); }
/// Returns the number of **bytes** available for writing without reallocation.
### primitives/src/transaction.rs
@@ -359,7 +359,6 @@ pub(crate) trait TxIdentifier: AsRef<[u8]> {}
impl TxIdentifier for Txid {}
impl TxIdentifier for Wtxid {}
-// Duplicated in `bitcoin`.
/// The marker MUST be a 1-byte zero value: 0x00. (BIP-0141)
#[cfg(feature = "alloc")]
const SEGWIT_MARKER: u8 = 0x00;
### primitives/src/witness.rs
@@ -135,6 +135,12 @@ impl Witness {
/// Constructs a new [`Witness`] object from a slice of bytes slices where each slice is a witness item.
///
+ /// # Panics
+ ///
+ /// Panics if the starting offset of the last element within encoded bytes exceeds `u32::MAX`. When
+ /// the sum of the lengths of the elements before the last one, including their length prefixes,
+ /// is larger than that maximum.
+ ///
/// # Examples
/// ```
/// use bitcoin_primitives::witness::Witness;
@@ -144,6 +150,7 @@ impl Witness {
/// assert_eq!(witness.get(0), Some(b"A".as_slice()));
/// assert_eq!(witness.get(3), Some(b"D".as_slice()));
/// ```
+ #[track_caller]
pub fn from_slice<T: AsRef<[u8]>>(slice: &[T]) -> Self {
let witness_elements = slice.len();
let index_size = witness_elements * 4;
@@ -220,6 +227,10 @@ impl Witness {
/// Pushes a new element on the witness, requires an allocation.
///
+ /// # Panics
+ ///
+ /// Panics if the witness would've held more than `u32::MAX` bytes of elements after the push.
+ ///
/// # Examples
///
/// ```rust
@@ -236,11 +247,13 @@ impl Witness {
/// assert_eq!(witness.last(), Some(witness_script.as_slice()));
/// ```
#[inline]
+ #[track_caller]
pub fn push<T: AsRef<[u8]>>(&mut self, new_element: T) {
self.push_slice(new_element.as_ref());
}
/// Pushes a new element slice onto the witness stack.
+ #[track_caller]
fn push_slice(&mut self, new_element: &[u8]) {
self.witness_elements += 1;
let previous_content_end = self.indices_start;
@@ -346,8 +359,8 @@ impl Witness {
}
/// Correctness Requirements: value must always fit within u32
-// This is duplicated in `bitcoin::blockdata::witness`, if you change it please do so over there also.
#[inline]
+#[track_caller]
fn encode_cursor(bytes: &mut [u8], start_of_indices: usize, index: usize, value: usize) {
let start = start_of_indices + index * 4;
let end = start + 4;Why this scored 21/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.