AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Moderate 62 Bitcoin

Merge rust-bitcoin/rust-bitcoin#7128: units: use checked subtraction/addition assignment for NumOpResult

Public commit record

What the developer wrote

Authored by Andrew Poelstra

100/100 · Strong
Merge rust-bitcoin/rust-bitcoin#7128: units: use checked subtraction/addition assignment for NumOpResult

201eca08c7f57d0e24ea54336476ec6857473a8b units: use impl_checked_assign! macro for amount and SignedAmount (jrakibi)
2013eb88f6bb5a0fd15846801398b72d7ff2ac82 units: test NumOpResult += and -= overflow (jrakibi)
054045b4d1daf52656e54b32de780ad975696828 units: use checked addition assignment for NumOpResult (jrakibi)
76036ac493b69812f4908ca69bc435e5305b83f3 units: use checked subtraction assignment for NumOpResult (jrakibi)

Pull request description:

`NumOpResult` is supposed to prevent overflow, as mentioned in the docs.

However, addition and subtraction assignment (`+=` and `-=`) currently use the wrapped type regular subtraction/addition instead of **_checked_** subtraction/addition. This can silenly wrap in release builds whilekeeping the result `Valid`.

This change adds checked addition and subtraction for `FeeRate`, `Weight`, `u64`, and `i64`.
`Amount` and `SignedAmount` already handle overflow correctly, so we only change the implementation for these four types.

This is a breaking change since we replace the generic impl with implementations for the four types mentioned above.

Found while auditing `units` crate with Codex.


ACKs for top commit:
Kixunil:
ACK 201eca08c7f57d0e24ea54336476ec6857473a8b
tcharding:
ACK 201eca08c7f57d0e24ea54336476ec6857473a8b
apoelstra:
ACK 201eca08c7f57d0e24ea54336476ec6857473a8b; successfully ran local tests


Tree-SHA512: e221721d0a0c44814aa2321d19f6b62e92d7d98bb11a4ac4b3d90b6e2bd45d761d4075e207c556e861e158974122f2e6199a4b637c08843c039fc5b765c822b5
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Explains rationale or failure mode✓ Mentions testing or verification✓ Links an issue, advisory, or supporting reference✓ Names security-relevant behavior explicitly
The short version

What changed, and why it matters

This commit fixes a bug in the rust-bitcoin library's 'units' crate where the += and -= operators on a special result type (NumOpResult) could silently wrap around on overflow in release builds, instead of reporting an error as documented. The patch replaces the wrapping behavior with checked arithmetic for several numeric types including fee rates, weights, block intervals, and raw integers. It is a defensive fix that prevents incorrect values from being treated as valid.

Recommended action

Upgrade to a version of rust-bitcoin containing this commit if your code uses NumOpResult with += or -= on the affected types. Review any code that previously relied on the wrapping behavior, as this is a breaking change that now returns Error on overflow. Run the new unit tests to confirm the fix.

Security signals we found

01

Integer overflow silently wrapping while result remains marked Valid

02

Violation of documented overflow-prevention contract on NumOpResult

03

Potential for incorrect fee-rate/weight/block-interval calculations downstream

04

Fix uses checked_add/checked_sub and propagates MathErrorKind::Overflow

05

Breaking API change: generic impl replaced with concrete type implementations

Risk score

Why this scored 62/100

Our methodology →
Potential impact 18/30
Exploitability 12/25
Stealth signal 10/15
Affected reach 10/15
Confidence 8/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.