On Sat, 26 Sep 2026 10:24:19 GMT, Yunbo Zhang <[email protected]> wrote:

>> Please review this small fix.
>> 
>> **Problem:**
>> 
>> `MethodHandles.foldArguments` throws `ArrayIndexOutOfBoundsException` 
>> instead of `IllegalArgumentException` for negative `pos` values, which 
>> violates the API's exception contract. Extreme values such as 
>> `Integer.MAX_VALUE` can also overflow the existing addition-based bounds 
>> check.
>> 
>> **Fix:**
>> 
>> Validate that `foldPos` is non-negative and use:
>> 
>> 
>> foldPos <= targetType.parameterCount() - foldVals - foldArgs
>> 
>> 
>> **Testing:**
>> 
>> - Added tests for invalid positions and the valid boundary case where a 
>> zero-argument `void` combiner is folded at the end of the target parameter 
>> list.
>> - `make test TEST=test/jdk/java/lang/invoke`: 198 passed, 4 excluded, 0 
>> failed.
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Yunbo Zhang has updated the pull request incrementally with one additional 
> commit since the last revision:
> 
>   8367022: Parameterize invalid fold position tests

src/java.base/share/classes/java/lang/invoke/MethodHandles.java line 5928:

> 5926:         int foldVals = rtype == void.class ? 0 : 1;
> 5927:         boolean ok = foldPos >= 0 &&
> 5928:                 foldPos <= targetType.parameterCount() - foldVals - 
> foldArgs;

Also, I'm noticing now that the exception message is not correct for this 
failure case. I think we should probably just eagerly throw an exception here 
if this condition fails (with a message to that effect).

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/32618#discussion_r4157830185

Reply via email to