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: convert offset & length to int properly #9492

Closed
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
4 changes: 2 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function fromArrayLike(obj) {
}

function fromArrayBuffer(obj, byteOffset, length) {
byteOffset >>>= 0;
byteOffset = internalUtil.toInteger(byteOffset);

const maxLength = obj.byteLength - byteOffset;

Expand All @@ -248,7 +248,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
if (length === undefined) {
length = maxLength;
} else {
length >>>= 0;
length = internalUtil.toLength(length);
if (length > maxLength)
throw new RangeError("'length' is out of bounds");
}
Expand Down
18 changes: 18 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,21 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why the step If len is +∞, return 253-1. is in there when the min() operation makes it seem unnecessary. I can't see any difference if that condition is left out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. In our implementation I omitted that, as min itself is sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trevnorris I raised a PR tc39/ecma262#738. Let's see what tc39 people feel.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trevnorris And it got merged :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thefourtheye excellent!

*/
exports.toLength = function toLength(argument) {
const len = toInteger(argument);
return len <= 0 ? 0 : Math.min(len, Number.MAX_SAFE_INTEGER);
};
21 changes: 21 additions & 0 deletions test/parallel/test-buffer-creation-regression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'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);
32 changes: 32 additions & 0 deletions test/parallel/test-internal-util-toInteger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Flags: --expose-internals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably test for a wider range of values. test/parallel/test-net-internal.js has some good examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

'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);
});
35 changes: 35 additions & 0 deletions test/parallel/test-internal-util-toLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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);
});