Read the trie header at the view's byteOffset - #1
Open
openwong2kim wants to merge 1 commit into
Open
Conversation
The upstream unicode-trie constructor has two header paths: a Buffer path using readUInt32LE, and a DataView path for plain Uint8Arrays. This patch drops the Buffer path, so every caller now goes through `new DataView(data.buffer)` -- which ignores the view's byteOffset and reads from byte 0 of the underlying ArrayBuffer. That is not equivalent for Buffers. In node, `Buffer.from(str, 'base64')` returns a view into a shared 8 KiB pool whenever the result is smaller than half of Buffer.poolSize, so any earlier small allocation leaves the decoded trie at a non-zero byteOffset and the header is read from unrelated bytes. The generated trie in xterm.js's unicode-graphemes addon decodes to 3023 bytes, well under the 4096 byte pooling threshold, so it is always pooled. Pass byteOffset and byteLength so the header is read relative to the view, restoring the behaviour the dropped Buffer path had.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
patches/unicode-trie-typescript.diffdrops upstream unicode-trie's Buffer header path, leaving every caller on theDataViewpath:That isn't equivalent for
Bufferinputs.new DataView(buffer)starts at byte 0 of the underlyingArrayBufferand ignores the view'sbyteOffset, whereasreadUInt32LEis relative to the view.It matters because in node
Buffer.from(str, 'base64')returns a view into a shared 8 KiB pool whenever the result is smaller than half ofBuffer.poolSize. So any earlier small allocation in the process leaves the decoded trie at a non-zerobyteOffset, andhighStart/errorValue/uncompressedLengthget read from unrelated bytes.This passes
byteOffsetandbyteLengthto theDataView, restoring what the dropped Buffer path did.Impact
The generated
UnicodeProperties.tsin xterm.js'saddon-unicode-graphemesdecodes to 3023 bytes — under the 4096 byte pooling threshold, so it is always pooled and always exposed. Depending on where in the pool it lands you get silently wrong character widths (no error raised),Error: Data errorout of tiny-inflate at import time, or a multi-gigabyte allocation.One line repro against the published addon, on node v24.15.0:
Browsers are unaffected — they take the
atobbranch, which allocates atbyteOffset0.Why here as well as in xterm.js
xterm.js vendors the output of this patch chain. The same fix is proposed there in xtermjs/xterm.js#6080, but without this change the next regeneration would silently revert it.
Verification
I reproduced the chain locally (
patchwithunicode-trie-index.diff, thenunicode-trie-typescript.diff) and confirmed:The patch is regenerated with plain
diff, matching the existing format; the change folds into an existing hunk (3 insertions, 1 deletion).