Skip to content

Commit 73bafa0

Browse files
mscdexjasnell
authored andcommitted
buffer: fix ArrayBuffer checks
This commit fixes detection of ArrayBuffers from different V8 contexts. This is especially a problem for environments like nw.js where the node and browser V8 contexts are not shared. PR-URL: #8453 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent cfe8278 commit 73bafa0

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

lib/buffer.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Buffer.from = function(value, encodingOrOffset, length) {
103103
if (typeof value === 'number')
104104
throw new TypeError('"value" argument must not be a number');
105105

106-
if (value instanceof ArrayBuffer)
106+
if (isArrayBuffer(value))
107107
return fromArrayBuffer(value, encodingOrOffset, length);
108108

109109
if (typeof value === 'string')
@@ -234,9 +234,6 @@ function fromArrayLike(obj) {
234234
}
235235

236236
function fromArrayBuffer(obj, byteOffset, length) {
237-
if (!isArrayBuffer(obj))
238-
throw new TypeError('argument is not an ArrayBuffer');
239-
240237
byteOffset >>>= 0;
241238

242239
const maxLength = obj.byteLength - byteOffset;
@@ -267,7 +264,7 @@ function fromObject(obj) {
267264
}
268265

269266
if (obj) {
270-
if (obj.buffer instanceof ArrayBuffer || 'length' in obj) {
267+
if (isArrayBuffer(obj.buffer) || 'length' in obj) {
271268
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
272269
return new FastBuffer();
273270
}
@@ -354,7 +351,7 @@ function base64ByteLength(str, bytes) {
354351

355352
function byteLength(string, encoding) {
356353
if (typeof string !== 'string') {
357-
if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)
354+
if (ArrayBuffer.isView(string) || isArrayBuffer(string))
358355
return string.byteLength;
359356

360357
string = '' + string;

test/parallel/test-buffer-alloc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4+
const vm = require('vm');
45

56
const Buffer = require('buffer').Buffer;
67
const SlowBuffer = require('buffer').SlowBuffer;
@@ -1049,6 +1050,11 @@ assert.throws(() => {
10491050
// Regression test
10501051
assert.doesNotThrow(() => Buffer.from(new ArrayBuffer()));
10511052

1053+
// Test that ArrayBuffer from a different context is detected correctly
1054+
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
1055+
assert.doesNotThrow(() => Buffer.from(arrayBuf));
1056+
assert.doesNotThrow(() => Buffer.from({ buffer: arrayBuf }));
1057+
10521058
assert.throws(() => Buffer.alloc({ valueOf: () => 1 }),
10531059
/"size" argument must be a number/);
10541060
assert.throws(() => Buffer.alloc({ valueOf: () => -1 }),

test/parallel/test-buffer-bytelength.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require('../common');
44
const assert = require('assert');
55
const Buffer = require('buffer').Buffer;
66
const SlowBuffer = require('buffer').SlowBuffer;
7+
const vm = require('vm');
78

89
// coerce values to string
910
assert.strictEqual(Buffer.byteLength(32, 'latin1'), 2);
@@ -87,3 +88,7 @@ assert.strictEqual(Buffer.byteLength('Il était tué', 'binary'), 12);
8788
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
8889
assert.strictEqual(24, Buffer.byteLength('Il était tué', encoding));
8990
});
91+
92+
// Test that ArrayBuffer from a different context is detected correctly
93+
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
94+
assert.strictEqual(Buffer.byteLength(arrayBuf), 0);

0 commit comments

Comments
 (0)