Skip to content

Do not mutate caller's options object in bitN and string - #317

Merged
keichi merged 1 commit into
masterfrom
fix-bit-options-mutation
Oct 18, 2026
Merged

keichi merged 1 commit into
masterfrom
fix-bit-options-mutation

Conversation

@keichi

@keichi keichi commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Problem

bitN assigned options.length = size directly on the caller's options object, so reusing one options object across bit fields of different sizes made all of them use the last size. string() similarly wrote options.encoding onto the caller's object.

Repro

const o = {};
new Parser().bit4("a", o).bit8("b", o).bit4("c").parse(Buffer.from([0xab, 0xcd]));
// RangeError (reads 24 bits) instead of { a: 0xa, b: 0xbc, c: 0xd }

Fix

Copy the options object ({ ...options, length: size } / { ...options, encoding }) instead of mutating it. Added a regression test.

@keichi
keichi merged commit 52ae496 into master Oct 18, 2026
8 checks passed
@keichi
keichi deleted the fix-bit-options-mutation branch October 18, 2026 13:50
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