buffer: simplify Buffer.concat #64836
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 written
The 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.
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.
vedchaudhari
gibson042
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: