Skip to content

fix: parse typed-array and Node Buffer views by their exact byte range - #547

Merged
awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/finding-1-buffer-byteoffset
Sep 30, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/finding-1-buffer-byteoffset

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This closes review finding 1 from the PR #512 review map: ReadBufferStream mishandled typed-array and Node Buffer views into a shared backing buffer.

Some background on why this bites in practice. In Node, a Buffer is almost never a whole ArrayBuffer — it's a view into a shared allocation pool. fs.readFileSync on any file below 4096 bytes, Buffer.from(str, "base64"), Buffer.concat(...) results and every buf.subarray(...) all hand you bytes that start at a nonzero byteOffset inside a bigger backing buffer. Browsers hit the same shape whenever a Uint8Array view is carved out of a larger transfer buffer.

ReadBufferStream got this wrong in two cooperating ways. SplitDataView.addBuffer unwrapped buffer.buffer and adopted the whole pool, dropping byteOffset; and the constructor seeded its read offset from buffer.offset — a property only Node Buffers have (it equals byteOffset), so plain Uint8Array views and Buffers behaved differently on the same bytes. For a plain view the parse window was pool bytes [0, view.byteLength): junk before the view made readFile throw "Invalid DICOM file, expected header is missing" even though the view's own bytes are a perfectly valid file — and when another valid file happened to precede the view in the pool (two files read into one pool — routine in Node), readFile silently returned the wrong file's dataset. No error, same element count, wrong data.

The fix is in the ReadBufferStream constructor: an ArrayBuffer.isView(buffer) input is now adopted as exactly its viewed byte range, normalized to logical offset 0, and the read offset no longer reads the Node-only Buffer.offset property — the offset seed survives only for the documented BufferStream sub-stream pattern, which keeps new ReadBufferStream(stream, le, { stop }) working as before.

const raw = fs.readFileSync("small.dcm");     // pooled: raw.byteOffset > 0
const shifted = Buffer.concat([Buffer.alloc(97), raw]).subarray(97);

// before: threw "Invalid DICOM file, expected header is missing",
// or silently parsed a neighbouring file's bytes out of the pool
// after: parses identically to a plain copy of the same bytes
const dicomDict = DicomMessage.readFile(shifted);

The regression suite is test/issues/issue311-node-buffer-footgun.test.js, which already pinned this exact footgun for issues #311/#370: its KNOWN GAP #311 pooled-view-equals-exact-slice test is now unskipped and green, the junk-prefixed-pool test flips from "fails loudly" to "parses the view's own bytes", and a new case pins the Node Buffer.subarray shape from the finding. The catalog entry for #311 moves from gap to green. Full suite: 611 passed, 19 skipped, 630 total.

On master (0.52): the same constructor line and the same pool-adopting addBuffer are both present, so the pooled plain-view aliasing half of this defect exists there too; the Buffer-specific offset seed only cancels out because the whole pool is adopted. The finding is master-applicable as a hotfix candidate.

🤖 Generated with Claude Code

ReadBufferStream adopted a view's whole backing ArrayBuffer and dropped
the byteOffset, so a pooled Uint8Array view either failed the DICM
header check against pool junk or silently parsed a neighbouring
file's bytes. The Node-only Buffer.offset property also seeded the
read offset, which only cancelled out while the whole pool was
adopted. The constructor now adopts exactly the viewed byte range,
normalized to logical offset 0, and seeds the offset from the source
only when it is a BufferStream sub-stream.

Closes review finding 1 of the PR #512 review map, and flips the
KNOWN GAP #311 pooled-view equivalence test to green.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 force-pushed the core/finding-1-buffer-byteoffset branch from 786e011 to faea45b Compare September 30, 2026 11:39
@awatson1978
awatson1978 merged commit 70df903 into release/1.0-beta-core Sep 30, 2026
8 checks passed
@awatson1978
awatson1978 deleted the core/finding-1-buffer-byteoffset branch September 30, 2026 11:40
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant