From 6b2aa1a2b9ba2cc51eafd31099a5658c52902e3d Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 26 Nov 2016 09:53:38 -0800 Subject: [PATCH] Revert "buffer: convert offset & length to int properly" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ca37fa527f174b547893817fe8c67a3befa02317. A test provided by the commit fails on most (but not all) platforms on CI. PR-URL: https://github.com/nodejs/node/pull/9814 Ref: https://github.com/nodejs/node/pull/9492 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: Michaël Zasso Reviewed-By: Сковорода Никита Андреевич --- lib/buffer.js | 4 +-- lib/internal/util.js | 18 ---------- .../test-buffer-creation-regression.js | 21 ----------- test/parallel/test-internal-util-toInteger.js | 32 ----------------- test/parallel/test-internal-util-toLength.js | 35 ------------------- 5 files changed, 2 insertions(+), 108 deletions(-) delete mode 100644 test/parallel/test-buffer-creation-regression.js delete mode 100644 test/parallel/test-internal-util-toInteger.js delete mode 100644 test/parallel/test-internal-util-toLength.js diff --git a/lib/buffer.js b/lib/buffer.js index 94bf9cdca3..b2325098bc 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -238,7 +238,7 @@ function fromArrayLike(obj) { } function fromArrayBuffer(obj, byteOffset, length) { - byteOffset = internalUtil.toInteger(byteOffset); + byteOffset >>>= 0; const maxLength = obj.byteLength - byteOffset; @@ -248,7 +248,7 @@ function fromArrayBuffer(obj, byteOffset, length) { if (length === undefined) { length = maxLength; } else { - length = internalUtil.toLength(length); + length >>>= 0; if (length > maxLength) throw new RangeError("'length' is out of bounds"); } diff --git a/lib/internal/util.js b/lib/internal/util.js index ae8b1e0b64..4ada8dd0cc 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -161,21 +161,3 @@ exports.cachedResult = function cachedResult(fn) { return result; }; }; - -/* - * Implementation of ToInteger as per ECMAScript Specification - * Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger - */ -const toInteger = exports.toInteger = function toInteger(argument) { - const number = +argument; - return Number.isNaN(number) ? 0 : Math.trunc(number); -}; - -/* - * Implementation of ToLength as per ECMAScript Specification - * Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tolength - */ -exports.toLength = function toLength(argument) { - const len = toInteger(argument); - return len <= 0 ? 0 : Math.min(len, Number.MAX_SAFE_INTEGER); -}; diff --git a/test/parallel/test-buffer-creation-regression.js b/test/parallel/test-buffer-creation-regression.js deleted file mode 100644 index 3432d81bf6..0000000000 --- a/test/parallel/test-buffer-creation-regression.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -require('../common'); -const assert = require('assert'); - -function test(size, offset, length) { - const arrayBuffer = new ArrayBuffer(size); - - const uint8Array = new Uint8Array(arrayBuffer, offset, length); - for (let i = 0; i < length; i += 1) { - uint8Array[i] = 1; - } - - const buffer = Buffer.from(arrayBuffer, offset, length); - for (let i = 0; i < length; i += 1) { - assert.strictEqual(buffer[i], 1); - } -} - -test(200, 50, 100); -test(8589934592 /* 1 << 40 */, 4294967296 /* 1 << 39 */, 1000); diff --git a/test/parallel/test-internal-util-toInteger.js b/test/parallel/test-internal-util-toInteger.js deleted file mode 100644 index 57a411964d..0000000000 --- a/test/parallel/test-internal-util-toInteger.js +++ /dev/null @@ -1,32 +0,0 @@ -// Flags: --expose-internals -'use strict'; - -require('../common'); -const assert = require('assert'); -const {toInteger} = require('internal/util'); - -const expectZero = [ - '0', '-0', NaN, {}, [], {'a': 'b'}, [1, 2], '0x', '0o', '0b', false, - '', ' ', undefined, null -]; -expectZero.forEach(function(value) { - assert.strictEqual(toInteger(value), 0); -}); - -assert.strictEqual(toInteger(Infinity), Infinity); -assert.strictEqual(toInteger(-Infinity), -Infinity); - -const expectSame = [ - '0x100', '0o100', '0b100', 0x100, -0x100, 0o100, -0o100, 0b100, -0b100, true -]; -expectSame.forEach(function(value) { - assert.strictEqual(toInteger(value), +value, `${value} is not an Integer`); -}); - -const expectIntegers = new Map([ - [[1], 1], [[-1], -1], [['1'], 1], [['-1'], -1], - [3.14, 3], [-3.14, -3], ['3.14', 3], ['-3.14', -3], -]); -expectIntegers.forEach(function(expected, value) { - assert.strictEqual(toInteger(value), expected); -}); diff --git a/test/parallel/test-internal-util-toLength.js b/test/parallel/test-internal-util-toLength.js deleted file mode 100644 index ce594c47c1..0000000000 --- a/test/parallel/test-internal-util-toLength.js +++ /dev/null @@ -1,35 +0,0 @@ -// Flags: --expose-internals -'use strict'; - -require('../common'); -const assert = require('assert'); -const {toLength} = require('internal/util'); -const maxValue = Number.MAX_SAFE_INTEGER; - -const expectZero = [ - '0', '-0', NaN, {}, [], {'a': 'b'}, [1, 2], '0x', '0o', '0b', false, - '', ' ', undefined, null, -1, -1.25, -1.1, -1.9, -Infinity -]; -expectZero.forEach(function(value) { - assert.strictEqual(toLength(value), 0); -}); - -assert.strictEqual(toLength(maxValue - 1), maxValue - 1); -assert.strictEqual(maxValue, maxValue); -assert.strictEqual(toLength(Infinity), maxValue); -assert.strictEqual(toLength(maxValue + 1), maxValue); - - -[ - '0x100', '0o100', '0b100', 0x100, -0x100, 0o100, -0o100, 0b100, -0b100, true -].forEach(function(value) { - assert.strictEqual(toLength(value), +value > 0 ? +value : 0); -}); - -const expectIntegers = new Map([ - [[1], 1], [[-1], 0], [['1'], 1], [['-1'], 0], - [3.14, 3], [-3.14, 0], ['3.14', 3], ['-3.14', 0], -]); -expectIntegers.forEach(function(expected, value) { - assert.strictEqual(toLength(value), expected); -});