Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
}

function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
function _copyActual(source, target, targetStart, sourceStart, sourceEnd, isUint8Copy = false) {
if (sourceEnd - sourceStart > target.byteLength - targetStart)
sourceEnd = sourceStart + target.byteLength - targetStart;

Expand All @@ -252,7 +252,11 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
if (nb <= 0)
return 0;

_copy(source, target, targetStart, sourceStart, nb);
if (sourceStart === 0 && nb === sourceLen && (isUint8Copy || isUint8Array(target))) {
TypedArrayPrototypeSet(target, source, targetStart);
} else {
_copy(source, target, targetStart, sourceStart, nb);
}

return nb;
}
Expand Down Expand Up @@ -600,7 +604,7 @@ Buffer.concat = function concat(list, length) {
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
}
pos += _copyActual(buf, buffer, pos, 0, buf.length);
pos += _copyActual(buf, buffer, pos, 0, buf.length, true);
}

// Note: `length` is always equal to `buffer.length` at this point
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-buffer-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ assert.throws(() => {
const random10 = common.hasCrypto ?
require('crypto').randomBytes(10) :
Buffer.alloc(10, 1);
const derived18 = Buffer.alloc(18);
for (let i = 0, j = 0; i < 18; i++) {
if (i < 10)
derived18[i] = random10[i];
else
derived18[i] = random10[j++];
}
const empty = Buffer.alloc(0);

assert.notDeepStrictEqual(random10, empty);
Expand All @@ -85,6 +92,7 @@ assert.deepStrictEqual(Buffer.concat([], 100), empty);
assert.deepStrictEqual(Buffer.concat([random10], 0), empty);
assert.deepStrictEqual(Buffer.concat([random10], 10), random10);
assert.deepStrictEqual(Buffer.concat([random10, random10], 10), random10);
assert.deepStrictEqual(Buffer.concat([random10, random10], 18), derived18);
assert.deepStrictEqual(Buffer.concat([empty, random10]), random10);
assert.deepStrictEqual(Buffer.concat([random10, empty, empty]), random10);

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-buffer-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ b.copy(c, 'not a valid offset');
// Make sure this acted like a regular copy with `0` offset.
assert.deepStrictEqual(c, b.slice(0, c.length));

// Copy into a Uint16Array target; bytes are packed into 16-bit elements.
{
const x = new Uint16Array(4);
const buf = Buffer.of(1, 2, 3, 4);
const copied = buf.copy(x);
assert.strictEqual(copied, 4);
assert.ok(x instanceof Uint16Array);
const bytes = new Uint8Array(x.buffer, x.byteOffset, 4);
assert.deepStrictEqual(Array.from(bytes), [1, 2, 3, 4]);
const remaining = new Uint8Array(
x.buffer,
x.byteOffset + 4,
x.byteLength - 4
);
assert.ok(remaining.every((b) => b === 0));
}

{
c.fill('C');
assert.throws(() => {
Expand Down
Loading