Repository navigation
Conversation
Signed-off-by: Richard Gibson <richard.gibson@gmail.com>
bef937d to
23a8bca
Compare
vedchaudhari
left a comment
There was a problem hiding this comment.
Nice cleanup! The Uint8Array cases behave the same as main in my testing (with and without length). Left one comment about other TypedArrays slipping through.
There was a problem hiding this comment.
I think dropping the isUint8Array() check lets any TypedArray through now, since TypedArrayPrototypeGetByteLength() doesn't throw for them. And the result can end up with uninitialized bytes:
Buffer.concat([new Uint16Array([1, 2, 3, 4])]);
// main: throws ERR_INVALID_ARG_TYPE
// here: 8-byte buffer, but only the first 4 bytes get writtenThe length is counted in bytes (8), but TypedArrayPrototypeSet() copies per element (4 values), and allocate() doesn't zero memory, so the last 4 bytes are whatever was in the pool. I tried it with a bigger input (new Uint16Array(4096)) and the unwritten half sometimes had leftover heap data. BigInt64Array also changes, from ERR_INVALID_ARG_TYPE to a plain TypeError: Cannot mix BigInt....
Keeping the isUint8Array() check in this loop should fix it. A test with something like a Uint16Array in test-buffer-concat.js would be nice too, since only non-TypedArray inputs are checked there right now.
Merge the auto-length and explicit-length paths of Buffer.concat while maintaining the branch-free status of the copy loop body from #61721 by discovering the the index of the first not-fully-copyable input inside the validation loop.
Also improve efficiency by relying upon TypedArrayPrototypeGetByteLength for input validation and capturing positions from it.
The end result is more unified code whose performance seems to be roughly equivalent and perhaps slightly skewed in favor of unspecified length: