mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 15:32:15 +00:00

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 ca37fa527f
broke
CI.
Refer: https://github.com/nodejs/node/pull/9814
Refer: https://github.com/nodejs/node/pull/9492
PR-URL: https://github.com/nodejs/node/pull/9815
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
33 lines
928 B
JavaScript
33 lines
928 B
JavaScript
// 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);
|
|
});
|