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

Merge rust-bitcoin/rust-bitcoin#2435: Enforce `$source: std::error::Error` in `write_err`

Public commit record

What the developer wrote

Authored by Andrew Poelstra

100/100 · Strong
Merge rust-bitcoin/rust-bitcoin#2435: Enforce `$source: std::error::Error` in `write_err`

71eea3d7f3c15ecae1360ded73c9710aec563e30 Enforce `$source: std::error::Error` in `write_err` (Martin Habovstiak)

Pull request description:

Sometimes people accidentally use `write_err` where `write` should be used or forget to implement `std::error::Error` for their types. So far we've relied mainly on review to catch these but it's difficult to see them and as this commit demonstrates one case slipped in #1400.

This commit add a check to `write_err` that enforces implementation of `std::error::Error + 'static` on types passed to `write_err` as `$source`. This doesn't trigger on `no_std` builds but does in `std` builds. While this would normally be weird in this case it's OK because `std` is tested.

There are still cases where this check is a false positive so the original macro is preserved as `write_err_unchecked_source`.

This change simultaneusly fixes the horrible bug introduced in #1400 which would cause infinite recursion when displaying error on `no_std`.


ACKs for top commit:
apoelstra:
ACK 71eea3d7f3c15ecae1360ded73c9710aec563e30; successfully ran local tests


Tree-SHA512: d9ea970b6b55d39334efa6d4baf3f366f6508d1ed77c8eb53d861c01e94a4b6cf47bd47d748ad6cec76224fc381b67802f6e2fa5ce4ab3db526ff9eda3e3db3e
✓ 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
The short version

What changed, and why it matters

This commit tightens a Rust helper macro used to format error messages so it only accepts real error types. It also fixes a bug where, in builds without the standard library, displaying certain errors would call itself forever (infinite recursion) and crash the program. Most of the changes are defensive compile-time checks; the actual recursion bug is mentioned in the pull request description but the diff only shows the macro change, not the original buggy code path.

Recommended action

Review the remaining `write_err_unchecked_source!` call sites to confirm they are truly generic and cannot be constrained to `std::error::Error`. Verify that the no_std infinite recursion path described in the PR is fully resolved by this macro change and that no other call sites still trigger recursion.

Security signals we found

01

Fixes infinite recursion in error Display on no_std builds (per PR description)

02

Adds compile-time enforcement that error-formatting macro receives an actual std::error::Error source

03

Prevents accidental misuse of write_err for non-error sources

04

Preserves unchecked variant for legitimate generic cases

Risk score

Why this scored 31/100

Our methodology →
Potential impact 8/30
Exploitability 3/25
Stealth signal 4/15
Affected reach 5/15
Confidence 7/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.