Fix byte count in NodeAnyTypedArray.withUnsafeMutableRawBytes#60
Open
liautaud wants to merge 1 commit into
Open
Fix byte count in NodeAnyTypedArray.withUnsafeMutableRawBytes#60liautaud wants to merge 1 commit into
NodeAnyTypedArray.withUnsafeMutableRawBytes#60liautaud wants to merge 1 commit into
Conversation
`napi_get_typedarray_info` returns the typed array's *element* count in its `length` out-parameter, not its byte count. The previous implementation passed that element count straight into the `UnsafeMutableRawBufferPointer` it handed back, which silently truncated the buffer for any non-byte typed array.
Author
|
(cc. @kabiroberai) |
NodeAnyTypedArray.withUnsafeMutableRawBytes.NodeAnyTypedArray.withUnsafeMutableRawBytes
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.
Summary
NodeAnyTypedArray.withUnsafeMutableRawBytescurrently passes thelengthvalue returned bynapi_get_typedarray_infostraight intoUnsafeMutableRawBufferPointer(start:count:). Per the Node-API docs, that value is the element count of the typed array, not its byte count. The two only coincide forInt8/UInt8/Uint8Clampedarrays.For every other typed-array kind, the returned raw buffer is silently truncated. This is particularly visible through
NodeTypedArray<Element>.withUnsafeMutableBytes, which callsbindMemory(to: Element.self)on the returned raw buffer: the resultingUnsafeMutableBufferPointer<Element>reports a count divided byMemoryLayout<Element>.size, so callers only see a fraction of the actual elements (and any reads/writes beyond that fraction are silently skipped).Fix
napi_get_typedarray_info(instead of passingnilfor thetypeout-parameter).UnsafeMutableRawBufferPointerthat covers the entire typed array.A new
byteSizeproperty onNodeTypedArrayKindcentralises the element-size mapping and mirrors the existingrawproperty.Repro
Notes
Int8/UInt8/Uint8Clampedarrays.// TODO: Do we need to advance data by offset/a multiple of offset?is removed;napi_get_typedarray_infoalready returnsdataadjusted by the byte offset, per the Node-API spec.