Skip to content

buf_encoder: change "early range check" assertion on max to one on min - #262

Open
apoelstra wants to merge 2 commits into
rust-bitcoin:masterfrom
apoelstra:2026-09/buf-encoder-max
Open

apoelstra wants to merge 2 commits into
rust-bitcoin:masterfrom
apoelstra:2026-09/buf-encoder-max

Conversation

@apoelstra

Copy link
Copy Markdown
Member

We have an assertion in buf_encoder whose purpose is to provide an early range-check to (hopefully) prod the compiler into eliding range checks inside a loop.

But because we were asserting on the iterator's size_hint maximum, which might overstate the iterator's length, this extra assertion accidentally restricted what iterators could be used with the function.

For the cases we can realistically expect optimizations, the minimum and maximum are the same, so just use the minimum instead. (Iterators are allowed to lie about their minimums, but at least when they do this they are misbehaving and it's reasonable that this would cause panics.)

Fixes https://github.com/project-loupe/audit-hex-conservative/issues/3

…`min`

We have an assertion in `buf_encoder` whose purpose is to provide an
early range-check to (hopefully) prod the compiler into eliding range
checks inside a loop.

But because we were asserting on the iterator's `size_hint` maximum,
which might overstate the iterator's length, this extra assertion
accidentally restricted what iterators could be used with the function.

For the cases we can realistically expect optimizations, the minimum and
maximum are the same, so just use the minimum instead. (Iterators are
allowed to lie about their minimums, but at least when they do this they
are misbehaving and it's reasonable that this would cause panics.)
@apoelstra

Copy link
Copy Markdown
Member Author

On d1874c8 successfully ran local tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant