Skip to content

Commit 0c745e3

Browse files
thefourtheyeFishrock123
authored andcommitted
buffer: convert offset & length to int properly
As per ecma-262 2015's #sec-%typedarray%-buffer-byteoffset-length, `offset` would be an integer, not a 32 bit unsigned integer. Also, `length` would be an integer with the maximum value of 2^53 - 1, not a 32 bit unsigned integer. This would be a problem because, if we create a buffer from an arraybuffer, from an offset which is greater than 2^32, it would be actually pointing to a different location in arraybuffer. For example, if we use 2^40 as offset, then the actual value used will be 0, because `byteOffset >>>= 0` will convert `byteOffset` to a 32 bit unsigned int, which is based on 2^32 modulo. This is a redo, as the ca37fa5 broke CI. Refer: #9814 Refer: #9492 PR-URL: #9815 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 5a2b688 commit 0c745e3

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

lib/buffer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function fromArrayLike(obj) {
228228
}
229229

230230
function fromArrayBuffer(obj, byteOffset, length) {
231-
byteOffset >>>= 0;
231+
byteOffset = internalUtil.toInteger(byteOffset);
232232

233233
const maxLength = obj.byteLength - byteOffset;
234234

@@ -238,7 +238,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
238238
if (length === undefined) {
239239
length = maxLength;
240240
} else {
241-
length >>>= 0;
241+
length = internalUtil.toLength(length);
242242
if (length > maxLength)
243243
throw new RangeError("'length' is out of bounds");
244244
}

lib/internal/util.js

+18
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,21 @@ exports.cachedResult = function cachedResult(fn) {
161161
return result;
162162
};
163163
};
164+
165+
/*
166+
* Implementation of ToInteger as per ECMAScript Specification
167+
* Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger
168+
*/
169+
const toInteger = exports.toInteger = function toInteger(argument) {
170+
const number = +argument;
171+
return Number.isNaN(number) ? 0 : Math.trunc(number);
172+
};
173+
174+
/*
175+
* Implementation of ToLength as per ECMAScript Specification
176+
* Refer: http://www.ecma-international.org/ecma-262/6.0/#sec-tolength
177+
*/
178+
exports.toLength = function toLength(argument) {
179+
const len = toInteger(argument);
180+
return len <= 0 ? 0 : Math.min(len, Number.MAX_SAFE_INTEGER);
181+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
6+
function test(arrayBuffer, offset, length) {
7+
const uint8Array = new Uint8Array(arrayBuffer, offset, length);
8+
for (let i = 0; i < length; i += 1) {
9+
uint8Array[i] = 1;
10+
}
11+
12+
const buffer = Buffer.from(arrayBuffer, offset, length);
13+
for (let i = 0; i < length; i += 1) {
14+
assert.strictEqual(buffer[i], 1);
15+
}
16+
}
17+
18+
const acceptableOOMErrors = [
19+
'Array buffer allocation failed',
20+
'Invalid array buffer length'
21+
];
22+
23+
const testCases = [
24+
[200, 50, 100],
25+
[4294967296 /* 1 << 32 */, 2147483648 /* 1 << 31 */, 1000],
26+
[8589934592 /* 1 << 33 */, 4294967296 /* 1 << 32 */, 1000]
27+
];
28+
29+
for (let index = 0, arrayBuffer; index < testCases.length; index += 1) {
30+
const [size, offset, length] = testCases[index];
31+
32+
try {
33+
arrayBuffer = new ArrayBuffer(size);
34+
} catch (e) {
35+
if (e instanceof RangeError && acceptableOOMErrors.includes(e.message))
36+
return common.skip(`Unable to allocate ${size} bytes for ArrayBuffer`);
37+
throw e;
38+
}
39+
40+
test(arrayBuffer, offset, length);
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
const assert = require('assert');
6+
const {toInteger} = require('internal/util');
7+
8+
const expectZero = [
9+
'0', '-0', NaN, {}, [], {'a': 'b'}, [1, 2], '0x', '0o', '0b', false,
10+
'', ' ', undefined, null
11+
];
12+
expectZero.forEach(function(value) {
13+
assert.strictEqual(toInteger(value), 0);
14+
});
15+
16+
assert.strictEqual(toInteger(Infinity), Infinity);
17+
assert.strictEqual(toInteger(-Infinity), -Infinity);
18+
19+
const expectSame = [
20+
'0x100', '0o100', '0b100', 0x100, -0x100, 0o100, -0o100, 0b100, -0b100, true
21+
];
22+
expectSame.forEach(function(value) {
23+
assert.strictEqual(toInteger(value), +value, `${value} is not an Integer`);
24+
});
25+
26+
const expectIntegers = new Map([
27+
[[1], 1], [[-1], -1], [['1'], 1], [['-1'], -1],
28+
[3.14, 3], [-3.14, -3], ['3.14', 3], ['-3.14', -3],
29+
]);
30+
expectIntegers.forEach(function(expected, value) {
31+
assert.strictEqual(toInteger(value), expected);
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
const assert = require('assert');
6+
const {toLength} = require('internal/util');
7+
const maxValue = Number.MAX_SAFE_INTEGER;
8+
9+
const expectZero = [
10+
'0', '-0', NaN, {}, [], {'a': 'b'}, [1, 2], '0x', '0o', '0b', false,
11+
'', ' ', undefined, null, -1, -1.25, -1.1, -1.9, -Infinity
12+
];
13+
expectZero.forEach(function(value) {
14+
assert.strictEqual(toLength(value), 0);
15+
});
16+
17+
assert.strictEqual(toLength(maxValue - 1), maxValue - 1);
18+
assert.strictEqual(maxValue, maxValue);
19+
assert.strictEqual(toLength(Infinity), maxValue);
20+
assert.strictEqual(toLength(maxValue + 1), maxValue);
21+
22+
23+
[
24+
'0x100', '0o100', '0b100', 0x100, -0x100, 0o100, -0o100, 0b100, -0b100, true
25+
].forEach(function(value) {
26+
assert.strictEqual(toLength(value), +value > 0 ? +value : 0);
27+
});
28+
29+
const expectIntegers = new Map([
30+
[[1], 1], [[-1], 0], [['1'], 1], [['-1'], 0],
31+
[3.14, 3], [-3.14, 0], ['3.14', 3], ['-3.14', 0],
32+
]);
33+
expectIntegers.forEach(function(expected, value) {
34+
assert.strictEqual(toLength(value), expected);
35+
});

0 commit comments

Comments
 (0)