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).

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

Commit messages:
 - 8367022: MethodHandles.foldArguments handle negative pos

Changes: https://git.openjdk.org/jdk/pull/32618/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=32618&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8367022
  Stats: 32 lines in 2 files changed: 29 ins; 0 del; 3 mod
  Patch: https://git.openjdk.org/jdk/pull/32618.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/32618/head:pull/32618

PR: https://git.openjdk.org/jdk/pull/32618

Reply via email to