On Fri, 2 Feb 2024 16:40:45 GMT, Raffaello Giulietti <[email protected]>
wrote:
>> src/java.base/share/classes/java/lang/String.java line 2506:
>>
>>> 2504: fromIndex = Math.max(0, fromIndex);
>>> 2505: return isLatin1() ? StringLatin1.indexOf(value, ch,
>>> fromIndex, value.length)
>>> 2506: : StringUTF16.indexOf(value, ch, fromIndex,
>>> value.length >> 1);
>>
>> This needs to include the check for `fromIndex >= this.length()`:
>> Suggestion:
>>
>> fromIndex = Math.max(0, fromIndex);
>> int toIndex = length();
>> if (fromIndex >= toIndex) {
>> return -1;
>> }
>> return isLatin1()
>> ? StringLatin1.indexOf(value, ch, fromIndex, toIndex)
>> : StringUTF16.indexOf(value, ch, fromIndex, toIndex);
>
> I don't think so.
> If you deeply follow the invoked `indexOf()` methods, there's either a check
> later, or the loop conditions are false on entry (although I'm not sure about
> the intrinsic methods).
While not directly observable (all tests pass) then sadly the intrinsic
implementation emits a string range check that triggers a decompilation and
subsequent compilation without the intrinsic if you'd call with a `fromIndex`
with values greater than `length()` (equal to should be fine). I'll try out
some options here.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/17685#discussion_r1476907109