Skip to content

Commit ca37fa5

Browse files
committed
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. PR-URL: #9492 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 561eade commit ca37fa5

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

lib/buffer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ function fromArrayLike(obj) {
238238
}
239239

240240
function fromArrayBuffer(obj, byteOffset, length) {
241-
byteOffset >>>= 0;
241+
byteOffset = internalUtil.toInteger(byteOffset);
242242

243243
const maxLength = obj.byteLength - byteOffset;
244244

@@ -248,7 +248,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
248248
if (length === undefined) {
249249
length = maxLength;
250250
} else {
251-
length >>>= 0;
251+
length = internalUtil.toLength(length);
252252
if (length > maxLength)
253253
throw new RangeError("'length' is out of bounds");
254254
}

lib/internal/util.js

Lines changed: 18 additions & 0 deletions
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+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
function test(size, offset, length) {
7+
const arrayBuffer = new ArrayBuffer(size);
8+
9+
const uint8Array = new Uint8Array(arrayBuffer, offset, length);
10+
for (let i = 0; i < length; i += 1) {
11+
uint8Array[i] = 1;
12+
}
13+
14+
const buffer = Buffer.from(arrayBuffer, offset, length);
15+
for (let i = 0; i < length; i += 1) {
16+
assert.strictEqual(buffer[i], 1);
17+
}
18+
}
19+
20+
test(200, 50, 100);
21+
test(8589934592 /* 1 << 40 */, 4294967296 /* 1 << 39 */, 1000);
Lines changed: 32 additions & 0 deletions
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+
});
Lines changed: 35 additions & 0 deletions
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)