-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
ToLength suffers from off-by-one errors #2502
Comments
Isn't the max integer index 253 - 2? (Nm, I see it's ≤ there. Only for array indexes is the offset - 2) |
This comment has been minimized.
This comment has been minimized.
Be careful that, in several cases, it is perfectly reasonable to have an “index” that is equal to the “length” of the corresponding string/array. This is the case for RegExp#lastIndex (where it means that the end of the string has been attained), or of Array#slice. (In fact, in those two examples, the “index” is better viewed as a position between two consecutive items, where 0 means “just before the first item” and length means “just after the last item”.) |
That's a good point, but I think issues with lastIndex and slice are avoided by the maximum length of Strings and Arrays both being less than the maximum length of array-like objects. Maybe the fix is just to leave ToLength as it is and reduce the maximum value of integer index by one (to 253 - 2), updating ToIndex accordingly—and preserving the weirdness of Integer-Indexed exotic objects subjecting "9007199254740992" and other decimal representations of large numbers having exact Number values to the same special treatment as an integer index even though they cannot ever be a valid index. |
(found when working through semantics for #2501)
ToLength is described as returning a value "suitable for use as the length of an array-like object", but its algorithm clamps that to 253 - 1, which is the maximum possible value of an integer index and therefore one less than the maximum possible length of an array-like object. And some uses of it correspond with the description (LengthOfArrayLike and StringPad), while others correspond with the algorithm (ToIndex and RegExp retrieval of
lastIndex
).ToIndex itself seems to have a similar problem, with TypedArray and ArrayBuffer and SharedArrayBuffer and InitializeTypedArrayFromArrayBuffer using its output as a length.
I believe this concern does not yet manifest in practice due to the extremely large values in question, but it should still be addressed.
The text was updated successfully, but these errors were encountered: