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

Merge rust-bitcoin/rust-bitcoin#6941: consensus_encoding: do not drop encoders with an empty first chunk

Public commit record

What the developer wrote

Authored by Andrew Poelstra

91/100 · Strong
Merge rust-bitcoin/rust-bitcoin#6941: consensus_encoding: do not drop encoders with an empty first chunk

8eefb6abafb9cce2080e51c0eb94cbcbcb95a1e4 consensus_encoding: handle empty composite chunks in state transitions (zjubiology)
193bbae46ecc0c34cbc6f3bd64fe1e63fe22dc9e consensus_encoding: skip empty composite encoders (zjubiology)
66fbb6d993959cc2ddfe203c0a1d0d00c526dc6d consensus_encoding: clarify empty chunk contract (zjubiology)

Pull request description:

`IterEncoder::new` skips leading encoders by testing `current_chunk().is_empty()` on a freshly constructed encoder and discarding it if so. But `Encoder::current_chunk` is explicitly documented as "May return an empty slice", so an encoder is allowed to be:

current_chunk() -> []
advance() -> HasMore
current_chunk() -> [0xaa, 0xbb]

Such an encoder is dropped entirely by `new()` and its bytes are lost, i.e. composing encoders through `IterEncoder` changes the encoded output.

This is not hypothetical: `EncoderN` yields its first component's chunk first, so

IterEncoder::new([Encoder2::new(
BytesEncoder::without_length_prefix(&[]),
BytesEncoder::without_length_prefix(&[1, 2, 3]),
)])

encodes to `[]` instead of `[1, 2, 3]` on master.

The fix advances each encoder until it yields a non-empty chunk or reports `Finished`, rather than judging it by its first chunk. `advance()` already handled this correctly; it is moved onto the same helper so the two paths can't drift apart, and two comments claiming a non-existent "chunk must be non-empty" trait contract are removed.


ACKs for top commit:
apoelstra:
ACK 8eefb6abafb9cce2080e51c0eb94cbcbcb95a1e4; successfully ran local tests


Tree-SHA512: dcdf3613d734f5de9c33dda815d9836c1bf689a9ec5526f28373fe76ef522c5f1d208d7bbf1b7d4e4737f9904cbd131f5274bdd8972e9e36027f93da4f761853
✓ 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 commit fixes a bug in the way the rust-bitcoin library chains together small data encoders. Previously, if an encoder's first output chunk was empty but it later produced real bytes, the library would silently throw the encoder away and lose those bytes. The fix makes the library advance each encoder past any initial empty chunks before deciding whether to keep it. This could change the bytes produced when encoding certain Bitcoin network messages or other composite data structures.

Recommended action

Review any code paths that build `IterEncoder` or composite encoders with components that may initially yield empty chunks, especially in consensus-critical serialization. Upgrade to the patched version and run the new regression tests. Consider whether the changed output could affect compatibility with peers or stored data.

Security signals we found

01

Data-loss bug in serialization path: encoders with empty first chunk were dropped, causing incorrect encoded output

02

Violation of documented `Encoder` contract: `current_chunk` may return empty slice, but `IterEncoder::new` assumed it meant the encoder was empty

03

Potential for consensus-relevant encoding mismatch if composite encoders are used for transaction or network message serialization

04

No explicit security advisory or CVE referenced in commit materials

Risk score

Why this scored 64/100

Our methodology →
Potential impact 18/30
Exploitability 12/25
Stealth signal 10/15
Affected reach 12/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.