Skip to content

Commit 2cd2f3f

Browse files
committed
Fix allowed range of array indices
1 parent e8d4f91 commit 2cd2f3f

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

src/utils/__tests__/__snapshots__/rttc.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8606,7 +8606,7 @@ Object {
86068606
"left": Array [
86078607
2,
86088608
],
8609-
"right": 4294967296,
8609+
"right": 4294967295,
86108610
}
86118611
`;
86128612

src/utils/__tests__/rttc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ describe('Member expression type combinations:', () => {
292292
// Extra tests for array indices integral check.
293293
valid.push([arr, 0])
294294
valid.push([arr, 10])
295-
valid.push([arr, 2 ** 32 - 1])
295+
valid.push([arr, 2 ** 32 - 2])
296296
invalid.push([arr, -1])
297297
invalid.push([arr, 0.5])
298-
invalid.push([arr, 2 ** 32])
298+
invalid.push([arr, 2 ** 32 - 1])
299299

300300
test('Valid type combinations are OK', () => {
301301
valid.forEach(([left, right]: [Value, Value]) => {

src/utils/rttc.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ const typeOf = (v: Value) => {
3636

3737
const isNumber = (v: Value) => typeOf(v) === 'number'
3838
// See section 4 of https://2ality.com/2012/12/arrays.html
39+
// v >>> 0 === v checks that v is a valid unsigned 32-bit int
3940
// tslint:disable-next-line:no-bitwise
40-
const isUint32 = (v: Value) => isNumber(v) && v >>> 0 === v
41+
const isArrayIndex = (v: Value) => isNumber(v) && v >>> 0 === v && v < 2 ** 32 - 1
4142
const isString = (v: Value) => typeOf(v) === 'string'
4243
const isBool = (v: Value) => typeOf(v) === 'boolean'
4344
const isObject = (v: Value) => typeOf(v) === 'object'
@@ -98,7 +99,7 @@ export const checkMemberAccess = (node: es.Node, obj: Value, prop: Value) => {
9899
if (isObject(obj)) {
99100
return isString(prop) ? undefined : new TypeError(node, ' as prop', 'string', typeOf(prop))
100101
} else if (isArray(obj)) {
101-
return isUint32(prop) ? undefined : new TypeError(node, ' as prop', 'integer', typeOf(prop))
102+
return isArrayIndex(prop) ? undefined : new TypeError(node, ' as prop', 'integer', typeOf(prop))
102103
} else {
103104
return new TypeError(node, '', 'object or array', typeOf(obj))
104105
}

0 commit comments

Comments
 (0)