Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: make Buffer.from throw on DataView #48410

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ function fromArrayLike(obj) {
}

function fromObject(obj) {
if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) {
if (obj.length !== undefined) {
if (typeof obj.length !== 'number') {
return new FastBuffer();
}
Expand Down
18 changes: 13 additions & 5 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1087,15 +1087,23 @@ assert.throws(
'Uint8Array. Received undefined'
});

// The first argument must be of type string or an instance of:
// Buffer, ArrayBuffer, or Array or an Array-like Object.
assert.throws(() => Buffer.from(), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined'
});
assert.throws(() => Buffer.from(null), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
assert.throws(() => Buffer.from({ buffer: new ArrayBuffer(4) }), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
assert.throws(() => Buffer.from(new DataView(new ArrayBuffer(4))), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer, ArrayBuffer, or Array or an Array-like Object. Received null'
});

// Test prototype getters don't throw
Expand Down Expand Up @@ -1150,7 +1158,7 @@ Buffer.from(new ArrayBuffer());
// Test that ArrayBuffer from a different context is detected correctly.
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
Buffer.from(arrayBuf);
Buffer.from({ buffer: arrayBuf });
Buffer.from(new Uint8Array(arrayBuf));

assert.throws(() => Buffer.alloc({ valueOf: () => 1 }),
/"size" argument must be of type number/);
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-buffer-sharedarraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ assert.deepStrictEqual(arr_buf, ar_buf);
// Checks for calling Buffer.byteLength on a SharedArrayBuffer.
assert.strictEqual(Buffer.byteLength(sab), sab.byteLength);

Buffer.from({ buffer: sab }); // Should not throw.
// Should not throw.
Buffer.from(sab);
Buffer.from(new Uint8Array(sab));