Skip to content

Fix Buffer *Write methods to default omitted length to remaining space#6891

Open
says1117 wants to merge 1 commit into
cloudflare:mainfrom
says1117:fix/buffer-write-default-length
Open

Fix Buffer *Write methods to default omitted length to remaining space#6891
says1117 wants to merge 1 commit into
cloudflare:mainfrom
says1117:fix/buffer-write-default-length

Conversation

@says1117

Copy link
Copy Markdown

What

The encoding-specific Buffer write methods (asciiWrite, base64Write,
base64urlWrite, hexWrite, latin1Write, ucs2Write, utf8Write)
defaulted an omitted length to the full buffer size, then validated it
against the space remaining after offset. Any two-argument call with a
non-zero offset therefore threw:

RangeError [ERR_OUT_OF_RANGE]: The value of "length" is out of range.
It must be >= 0 && <= 9. Received 10

This breaks real callers - notably protobufjs ≥ 7.6.0, which calls
buf.utf8Write(val, pos) during encoding.

Fix

Default the omitted length to this.length - offset in all seven
methods, matching Node.js (which itself hit and reverted this exact bug
class between v22.7.0 and v22.8.0, nodejs/node#54523).

Testing

Adds writeMethodsDefaultLength to buffer-nodejs-test.js, exercising all
seven methods with a non-zero offset and omitted length, plus a byte-level
spot check for utf8Write. The expected results were cross-checked against
Node.js.

Fixes #6875

The encoding-specific write methods (asciiWrite, base64Write,
base64urlWrite, hexWrite, latin1Write, ucs2Write, utf8Write) defaulted an
omitted length argument to the full buffer size (	his.length) but then
validated it against the bytes remaining after offset
(	his.length - offset). As a result, any two-argument call with a
non-zero offset — e.g. �uf.utf8Write(str, pos) — always threw
ERR_OUT_OF_RANGE regardless of the string being written.

Default the omitted length to 	his.length - offset instead, matching
Node.js. Node briefly shipped this same bug class in v22.7.0
(nodejs/node#54523) and reverted it in v22.8.0 because it broke protobufjs
and others.

Adds a regression test covering all seven *Write methods.

Fixes cloudflare#6875
@says1117
says1117 requested review from a team as code owners July 21, 2026 09:15
@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@says1117

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Jul 21, 2026
@says1117

Copy link
Copy Markdown
Author

Hi! This is my first contribution, so the internal build workflow didn't run automatically on my fork. Could one of the assigned reviewers kick it off when you have a chance?

Quick summary of the change: the encoding-specific Buffer *Write methods (utf8Write, asciiWrite, etc.) defaulted an omitted length to the full buffer size but validated against the space remaining after offset, so any two-arg call with a non-zero offset threw ERR_OUT_OF_RANGE (breaks protobufjs). Fixed to default to this.length - offset, matching Node.js. Added a regression test covering all seven methods; verified the expected results against Node directly.

Thanks for taking a look! :)

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.

Buffer.prototype.utf8Write (and all other *Write methods) throw ERR_OUT_OF_RANGE when length is omitted and offset > 0`

1 participant