Merge rust-bitcoin/rust-bitcoin#7000: internals: add track_caller to U256 ops
What changed, and why it matters
This change adds Rust's #[track_caller] attribute to several internal math and array helper functions. It does not fix a bug or change any calculation; it only improves panic error messages so that if an overflow or division-by-zero happens in testing, the error points to the caller's code rather than deep inside the library. There is no security vulnerability being patched.
No security action required. Treat as a normal code-quality/debugging improvement.
Security signals we found
No vulnerability fix: only diagnostic attribute added
Overflow and division-by-zero checks already existed; behavior unchanged
No unsafe code modified
No advisory, CVE, or security discussion in commit
Evidence from the diff
The commit annotates U256 arithmetic operators (Add, Sub, Mul, Div, Rem) and ArrayVec constructors/extend methods with #[track_caller]. These functions already panic on invalid input via debug_assert! or explicit checks. track_caller propagates the caller’s location into panic messages, aiding debugging. No logic, bounds checks, or overflow behavior changed.
Changed components
internals/src/u256.rsinternals/src/array_vec.rsInspect captured patch +8 / −0
### internals/src/array_vec.rs
@@ -36,6 +36,7 @@ mod safety_boundary {
/// # Panics
///
/// If the slice is longer than `CAP`.
+ #[track_caller]
#[cfg_attr(creusot, check(ghost))]
#[cfg_attr(creusot, requires(slice@.len() <= CAP@))]
#[cfg_attr(creusot, ensures(result@ == slice@))]
@@ -296,6 +297,7 @@ impl<T: Copy, const CAP: usize> ArrayVec<T, CAP> {
/// # Panics
///
/// If the length would increase past CAP.
+ #[track_caller]
#[cfg_attr(creusot, requires(slice@.len() <= CAP@ - self@.len()))]
#[cfg_attr(creusot, ensures((^self)@ == (*self)@.concat(slice@)))]
pub fn extend_from_slice(&mut self, slice: &[T]) {
### internals/src/u256.rs
@@ -128,6 +128,7 @@ impl U256 {
///
/// If `rhs` is zero.
#[allow(clippy::indexing_slicing)]
+ #[track_caller]
fn div_rem(self, rhs: Self) -> (Self, Self) {
let mut sub_copy = self;
let mut shift_copy = rhs;
@@ -400,6 +401,7 @@ impl<T: Into<u128>> From<T> for U256 {
impl Add for U256 {
type Output = Self;
+ #[track_caller]
fn add(self, rhs: Self) -> Self {
let (res, overflow) = self.overflowing_add(rhs);
debug_assert!(!overflow, "addition of U256 values overflowed");
@@ -409,6 +411,7 @@ impl Add for U256 {
impl Sub for U256 {
type Output = Self;
+ #[track_caller]
fn sub(self, rhs: Self) -> Self {
let (res, overflow) = self.overflowing_sub(rhs);
debug_assert!(!overflow, "subtraction of U256 values overflowed");
@@ -418,6 +421,7 @@ impl Sub for U256 {
impl Mul for U256 {
type Output = Self;
+ #[track_caller]
fn mul(self, rhs: Self) -> Self {
let (res, overflow) = self.overflowing_mul(rhs);
debug_assert!(!overflow, "multiplication of U256 values overflowed");
@@ -427,11 +431,13 @@ impl Mul for U256 {
impl Div for U256 {
type Output = Self;
+ #[track_caller]
fn div(self, rhs: Self) -> Self { self.div_rem(rhs).0 }
}
impl Rem for U256 {
type Output = Self;
+ #[track_caller]
fn rem(self, rhs: Self) -> Self { self.div_rem(rhs).1 }
}
Why this scored 20/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.