Skip to content

Commit

Permalink
add length check
Browse files Browse the repository at this point in the history
  • Loading branch information
jkkang-kosac committed Nov 4, 2020
1 parent f44c2f2 commit be5cd35
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,15 @@ Buffer.concat = function concat (list, length) {
for (i = 0; i < list.length; ++i) {
const buf = list[i]
if (isInstance(buf, Uint8Array)) {
Uint8Array.prototype.set.call(
buffer,
buf,
pos
)
if (pos + buf.length > buffer.length) {
Buffer.from(buf).copy(buffer, pos)
} else {
Uint8Array.prototype.set.call(
buffer,
buf,
pos
)
}
} else if (!Buffer.isBuffer(buf)) {
throw new TypeError('"list" argument must be an Array of Buffers')
} else {
Expand Down
7 changes: 7 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ test('concat() works on Uint8Array instances', function (t) {
t.end()
})

test('concat() works on Uint8Array instances for smaller provided totalLength', function (t) {
const result = B.concat([new Uint8Array([1, 2]), new Uint8Array([3, 4])], 3)
const expected = B.from([1, 2, 3])
t.deepEqual(result, expected)
t.end()
})

test('fill', function (t) {
const b = new B(10)
b.fill(2)
Expand Down

0 comments on commit be5cd35

Please sign in to comment.