Editorial: handle len < seachLen in String.prototype.lastIndexOf #3660
+1
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In Step 9 of String.prototype.lastIndexOf, it tries to clamp pos between 0 and len - searchLen and according to clamping,
len - searchLen >= 0
. So it cannot be defined whenlen - searchLen < 0
.For example:
Even if clamping were defined to allow cases where the lower bound is greater than the upper bound (i.e.,
len - searchLen < 0
), Step 10 would result in an invalid state. In Step 10, StringLastIndexOf(S, searchStr, start) is invoked with start, which is required to be a non-negative integer. Whenlen < searchLen
, the computed start could become negative, which is invalid.Therefore, this PR adds a step that returns -1 when
len < searchLen
before Step 9, since it is impossible to find searchString within string whenlen < searchLen
.