Skip to content

Honor byteOffset when constructing the DataView#147

Merged
mourner merged 1 commit into
mapbox:mainfrom
mattico:fix/dataview-byteoffset
Jul 1, 2026
Merged

Honor byteOffset when constructing the DataView#147
mourner merged 1 commit into
mapbox:mainfrom
mattico:fix/dataview-byteoffset

Conversation

@mattico

@mattico mattico commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

PbfReader and PbfWriter build their DataView over this.buf.buffer but read/write fixed-width fields (double/float/fixed32/fixed64) at offsets relative to this.buf. When the backing view has a non-zero byteOffset (which Node Buffers from the shared pool routinely do) every fixed-width access lands at the wrong absolute offset and returns garbage (e.g. subnormal doubles). Varint/string reads were unaffected because they index this.buf directly.

Construct the DataView with the view's byteOffset and byteLength so this.pos (a buf-relative offset) resolves correctly.

Adds regression tests covering fixed-width read and write through a view with a non-zero byteOffset.

PbfReader and PbfWriter build their DataView over `this.buf.buffer` but
read/write fixed-width fields (double/float/fixed32/fixed64) at offsets
relative to `this.buf`. When the backing view has a non-zero byteOffset
(which Node Buffers from the shared pool routinely do) every
fixed-width access lands at the wrong absolute offset and returns
garbage. Varint/string reads were unaffected because they index
`this.buf` directly.

Construct the DataView with the view's byteOffset and byteLength so
`this.pos` (a buf-relative offset) resolves correctly.

Adds regression tests covering fixed-width read and write through a view
with a non-zero byteOffset.
@mattico mattico requested a review from a team as a code owner July 1, 2026 19:10
@mattico mattico requested review from ibesora and removed request for a team July 1, 2026 19:10

@mourner mourner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch, thanks for the contribution!

@mourner mourner merged commit a26e890 into mapbox:main Jul 1, 2026
1 check passed
@mattico mattico deleted the fix/dataview-byteoffset branch July 1, 2026 22:18
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