From 4e0757d210583e018de7349633888576f4856e9f Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sun, 11 Jun 2023 00:15:18 +0800 Subject: [PATCH 1/2] buffer: make Buffer.from throw on DataView --- lib/buffer.js | 2 +- test/parallel/test-buffer-alloc.js | 14 +++++++++++++- test/parallel/test-buffer-sharedarraybuffer.js | 4 +++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 0ff7c1920adbba..6a4c301c78c831 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -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(); } diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index c6b728027057ec..aeec20a7a5e10f 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -1097,6 +1097,18 @@ assert.throws(() => Buffer.from(null), { message: 'The first argument must be of type string or an instance of ' + 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received null' }); +assert.throws(() => Buffer.from({ buffer: new ArrayBuffer(4) }), { + 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 an instance of Object' +}); +assert.throws(() => Buffer.from(new DataView(new ArrayBuffer(4))), { + 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 an instance of DataView' +}); // Test prototype getters don't throw assert.strictEqual(Buffer.prototype.parent, undefined); @@ -1150,7 +1162,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/); diff --git a/test/parallel/test-buffer-sharedarraybuffer.js b/test/parallel/test-buffer-sharedarraybuffer.js index 52cec6e9c5cd1a..d5ccfde05dde7d 100644 --- a/test/parallel/test-buffer-sharedarraybuffer.js +++ b/test/parallel/test-buffer-sharedarraybuffer.js @@ -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)); From ee51a88134f1bed92f137e99d7da3676929ebc6a Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sun, 11 Jun 2023 03:57:48 +0800 Subject: [PATCH 2/2] squash: test for code, dont test for message --- test/parallel/test-buffer-alloc.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index aeec20a7a5e10f..bcdc97edb5e8a1 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -1087,27 +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', - message: 'The first argument must be of type string or an instance of ' + - 'Buffer, ArrayBuffer, or Array or an Array-like Object. Received null' }); assert.throws(() => Buffer.from({ buffer: 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 an instance of Object' }); 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 an instance of DataView' }); // Test prototype getters don't throw