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: fix ArrayBuffer checks #8453

Closed
wants to merge 1 commit 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
9 changes: 3 additions & 6 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Buffer.from = function(value, encodingOrOffset, length) {
if (typeof value === 'number')
throw new TypeError('"value" argument must not be a number');

if (value instanceof ArrayBuffer)
if (isArrayBuffer(value))
return fromArrayBuffer(value, encodingOrOffset, length);

if (typeof value === 'string')
Expand Down Expand Up @@ -234,9 +234,6 @@ function fromArrayLike(obj) {
}

function fromArrayBuffer(obj, byteOffset, length) {
if (!isArrayBuffer(obj))
throw new TypeError('argument is not an ArrayBuffer');

byteOffset >>>= 0;

const maxLength = obj.byteLength - byteOffset;
Expand Down Expand Up @@ -267,7 +264,7 @@ function fromObject(obj) {
}

if (obj) {
if (obj.buffer instanceof ArrayBuffer || 'length' in obj) {
if (isArrayBuffer(obj.buffer) || 'length' in obj) {
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
return new FastBuffer();
}
Expand Down Expand Up @@ -354,7 +351,7 @@ function base64ByteLength(str, bytes) {

function byteLength(string, encoding) {
if (typeof string !== 'string') {
if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)
if (ArrayBuffer.isView(string) || isArrayBuffer(string))
return string.byteLength;

string = '' + string;
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const vm = require('vm');

const Buffer = require('buffer').Buffer;
const SlowBuffer = require('buffer').SlowBuffer;
Expand Down Expand Up @@ -1049,6 +1050,11 @@ assert.throws(() => {
// Regression test
assert.doesNotThrow(() => Buffer.from(new ArrayBuffer()));

// Test that ArrayBuffer from a different context is detected correctly
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
assert.doesNotThrow(() => Buffer.from(arrayBuf));
assert.doesNotThrow(() => Buffer.from({ buffer: arrayBuf }));

assert.throws(() => Buffer.alloc({ valueOf: () => 1 }),
/"size" argument must be a number/);
assert.throws(() => Buffer.alloc({ valueOf: () => -1 }),
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-buffer-bytelength.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require('../common');
const assert = require('assert');
const Buffer = require('buffer').Buffer;
const SlowBuffer = require('buffer').SlowBuffer;
const vm = require('vm');

// coerce values to string
assert.strictEqual(Buffer.byteLength(32, 'latin1'), 2);
Expand Down Expand Up @@ -87,3 +88,7 @@ assert.strictEqual(Buffer.byteLength('Il était tué', 'binary'), 12);
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
assert.strictEqual(24, Buffer.byteLength('Il était tué', encoding));
});

// Test that ArrayBuffer from a different context is detected correctly
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
assert.strictEqual(Buffer.byteLength(arrayBuf), 0);