Skip to content

fix: preserve bitlist input when reusing bytes - #566

Closed
kutluhaneth46 wants to merge 1 commit into
ChainSafe:masterfrom
kutluhaneth46:fix/bitlist-reuseBytes-preserve-input
Closed

kutluhaneth46 wants to merge 1 commit into
ChainSafe:masterfrom
kutluhaneth46:fix/bitlist-reuseBytes-preserve-input

Conversation

@kutluhaneth46

Copy link
Copy Markdown

Motivation

With reuseBytes: true, decoding a non-byte-aligned bitlist clears its padding bit in the input buffer. Decoding the same bytes again can then return a different length or throw (see #559).

Description

Check the bit length against the type limit before slicing, and always copy when the padding bit must be cleared so the caller buffer stays intact. Byte-aligned paths still reuse the input buffer.

Adds a regression suite for BitListType and ProgressiveBitListType covering the two-decode case, over-limit rejection before slice/subarray, and Buffer inputs.

Fixes #559

Local: pnpm exec vitest run test/unit/bitList-reuseBytes.test.ts --coverage.enabled=false (34/34 PASS)

Non-byte-aligned BitList deserialize with reuseBytes cleared the
delimiter bit in the caller's buffer. Check the length limit before
slicing, and always copy when the padding bit must be cleared.

Fixes ChainSafe#559
@kutluhaneth46
kutluhaneth46 requested a review from a team as a code owner November 21, 2026 15:57
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T16:00:05.695373Z 1c0dd82 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@erkancamli

Copy link
Copy Markdown

This looks like the same fix as #563 (opened 19 Nov): the same limitBits parameter on deserializeUint8ArrayBitListFromBytes, the limit check moved before the copy, and the same unconditional slice for the non-byte-aligned case. Apart from the doc comment #563 adds to deserialize, the only differences are Infinity vs Number.POSITIVE_INFINITY and the explicit lastByte === 1 ? 0 branch, which gives the same value as lastByte.toString(2).length - 1.

I ran both test files against master, #563 (d16f42d) and this branch (1c0dd82): on master the reuse cases fail as expected, and on each branch all 86 tests across both files pass. So either PR fixes the bug and covers the other's tests. #563's file also checks a container whose other fields keep reusing the input while the bitlist is copied, so it may be simplest to land one of the two and carry over any test the other adds.

@kutluhaneth46

Copy link
Copy Markdown
Author

Thanks @erkancamli for checking both branches.

You are right. #563 opened first and lands the same fix (limit check before copy, unconditional slice when clearing the padding bit). Its doc comment on deserialize and the container reuse coverage are also useful. I will close this PR in favor of #563 so the maintainers only have one change to review.

Happy to pull any of my tests into #563 if anything is still missing there.

@kutluhaneth46

Copy link
Copy Markdown
Author

Closing as a duplicate of #563 (opened earlier with the same bitlist reuseBytes fix).

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.

BitList deserialization with reuseBytes: true mutates the input buffer

2 participants