Skip to content

Commit 6e3db28

Browse files
mscdexFishrock123
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> Conflicts: test/parallel/test-buffer-alloc.js
1 parent 09da575 commit 6e3db28

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
@@ -88,7 +88,7 @@ Buffer.from = function(value, encodingOrOffset, length) {
8888
if (typeof value === 'number')
8989
throw new TypeError('"value" argument must not be a number');
9090

91-
if (value instanceof ArrayBuffer)
91+
if (isArrayBuffer(value))
9292
return fromArrayBuffer(value, encodingOrOffset, length);
9393

9494
if (typeof value === 'string')
@@ -212,9 +212,6 @@ function fromArrayLike(obj) {
212212
}
213213

214214
function fromArrayBuffer(obj, byteOffset, length) {
215-
if (!isArrayBuffer(obj))
216-
throw new TypeError('argument is not an ArrayBuffer');
217-
218215
byteOffset >>>= 0;
219216

220217
const maxLength = obj.byteLength - byteOffset;
@@ -245,7 +242,7 @@ function fromObject(obj) {
245242
}
246243

247244
if (obj) {
248-
if (obj.buffer instanceof ArrayBuffer || 'length' in obj) {
245+
if (isArrayBuffer(obj.buffer) || 'length' in obj) {
249246
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
250247
return new FastBuffer();
251248
}
@@ -332,7 +329,7 @@ function base64ByteLength(str, bytes) {
332329

333330
function byteLength(string, encoding) {
334331
if (typeof string !== 'string') {
335-
if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)
332+
if (ArrayBuffer.isView(string) || isArrayBuffer(string))
336333
return string.byteLength;
337334

338335
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;
@@ -1078,3 +1079,8 @@ assert.throws(() => {
10781079

10791080
// Regression test
10801081
assert.doesNotThrow(() => Buffer.from(new ArrayBuffer()));
1082+
1083+
// Test that ArrayBuffer from a different context is detected correctly
1084+
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
1085+
assert.doesNotThrow(() => Buffer.from(arrayBuf));
1086+
assert.doesNotThrow(() => Buffer.from({ buffer: arrayBuf }));

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)