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

Merge rust-bitcoin/rust-bitcoin#7023: Evaluate macro arguments once in load_int_le and impl_hex

Public commit record

What the developer wrote

Authored by Andrew Poelstra

91/100 · Strong
Merge rust-bitcoin/rust-bitcoin#7023: Evaluate macro arguments once in load_int_le and impl_hex

f5ba7ffcc43a031b9c0a8b36ad78d90dc4902c2a internals: Test impl_hex evaluates the lookup once (PeterXMR)
13da54c82b8f468d3f8a3bb1e86095168e2b6bc6 internals: Evaluate impl_hex lookup table once (PeterXMR)
a792c1a96a05d6948a1ef962ad932e461f449314 hashes: Test load_int_le evaluates its arguments once (PeterXMR)
05721fc5aaf4c13a478e04cf7e43b7dcccc7ffbc hashes: Evaluate load_int_le arguments once (PeterXMR)

Pull request description:

Follow-up to the discussion on rust-bitcoin/hex-conservative#264, where apoelstra asked for an audit of rust-bitcoin for `macro_rules!` arguments that get evaluated more than once, and for a "hit count" unit test that locks the property in.

I checked every `macro_rules!` definition in the workspace for `expr`, `tt` and `block` captures that appear more than once in an expansion. Only two library macros re-evaluate a runtime argument:

* `load_int_le!` in `hashes/src/siphash24/mod.rs` expanded `$buf` twice and `$i` three times.
* `impl_hex!` in `internals/src/u256.rs` expanded `$lookup` twice inside the per-byte loop, so the lookup table expression was evaluated 64 times per formatted `U256`.

Everything else is either a compile-time position (array lengths, const generics), a place expression that cannot be bound (`round!` in sha256, `compress!` in siphash24), mutually exclusive branches (`write_err!`, `read_push_data_len!`), or a test-only macro, so those are left alone.

Both fixes bind the argument with `match $x { x => ... }` as discussed in the hex-conservative PR. Each fix commit is followed by a test commit that passes a counting block expression as the argument and asserts it is hit exactly once; without the fix commit the tests observe 2/3 and 64 evaluations respectively.

The audit and patches were produced with the help of an LLM (Claude Fable 5.1, see the `Assisted-by` trailers) and reviewed by me.


ACKs for top commit:
Kixunil:
ACK f5ba7ffcc43a031b9c0a8b36ad78d90dc4902c2a
apoelstra:
ACK f5ba7ffcc43a031b9c0a8b36ad78d90dc4902c2a; successfully ran local tests; these are easy fixes so might as well merge them even if we decide to fix the macros later


Tree-SHA512: 3a2fade9e8e0b0c3fd8e2b938f12ca1ae84f2757668b39456268b8668027683cf85fabf7e30d8d913123fdfa621fd54ba5d69d8d790a4e9bba4f70f6ec6f345e
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Mentions testing or verification✓ Links an issue, advisory, or supporting reference
The short version

What changed, and why it matters

This merge commit fixes two internal Rust macros that accidentally evaluated their arguments multiple times instead of once. In one case, a buffer and index used in a hashing function were evaluated 2-3 times per call; in the other, a hex-character lookup table was rebuilt 64 times every time a large number was formatted as hex. The patch binds each argument to a single variable using a match expression, and adds tests that prove the arguments are now evaluated exactly once. These are correctness/performance hygiene fixes rather than an obvious remote exploit, but repeated evaluation of user-supplied expressions in macros can hide subtle bugs or side effects.

Recommended action

Treat as a low-severity correctness fix. Review downstream callers of `load_int_le!` and `impl_hex!` to confirm no observable behavior change from removing duplicated side effects. Include the new unit tests in CI. No urgent security response is indicated by the supplied materials, but the pattern is worth auditing elsewhere in the workspace.

Security signals we found

01

Macro argument multiple-evaluation bug (code-quality / correctness)

02

Potential unexpected side effects from duplicated expression evaluation

03

Unsafe code path (`load_int_le!`) touched by macro change

04

Performance/DoS relevance: 64x redundant lookup construction per U256 format

05

No explicit security advisory or CVE referenced in commit materials

Risk score

Why this scored 32/100

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