Skip to content

fix(yaml-1.1/binary): stringify Uint8Array views, not their whole ArrayBuffer - #747

Open
simpleqt wants to merge 1 commit into
eemeli:mainfrom
simpleqt:fix/binary-uint8array-view
Open

simpleqt wants to merge 1 commit into
eemeli:mainfrom
simpleqt:fix/binary-uint8array-view

Conversation

@simpleqt

Copy link
Copy Markdown

The !!binary stringify path called Buffer.from(buf.buffer), which base64-encodes the whole underlying ArrayBuffer and ignores byteOffset/byteLength: stringify(new Uint8Array([9,9,0,1,2,255]).subarray(2), {version:'1.1'}) emitted all six bytes, and the document parses back as [9,9,0,1,2,255] instead of [0,1,2,255].

Pass byteOffset/byteLength to Buffer.from (src/schema/yaml-1.1/binary.ts:47), matching what the btoa path in the same function already does.

Regression test in tests/doc/types.ts fails on main 528ef30d and passes with this change. Full vitest suite with the json-test-suite/yaml-test-suite submodules: 3414 passed / 0 failed; eslint, prettier and tsc --noEmit clean.

…ayBuffer

When a Uint8Array that is a view (e.g. from subarray(), or constructed as
new Uint8Array(buffer, byteOffset, length)) is stringified as !!binary on
the Buffer path, Buffer.from(buf.buffer) wrapped the entire underlying
ArrayBuffer, so the emitted base64 covered bytes before the view and
beyond its length; parsing back gave the wrong bytes and the wrong count.

Pass buf.byteOffset and buf.byteLength to Buffer.from so only the view's
bytes are encoded, matching what the toBase64 and btoa paths already do
and what Buffer and a full Uint8Array produce.

Regression test: stringify(new Uint8Array([9, 9, 0, 1, 2, 255]).subarray(2))
now round-trips to the four bytes of the view instead of all six.

Signed-off-by: simpleqt <2015900818@qq.com>
Copilot AI balanced review requested due to automatic review settings November 22, 2026 17:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

2 participants