Skip to content

Fix ArrayUtils.reverse range underflow on Integer.MIN_VALUE end#1750

Merged
garydgregory merged 1 commit into
apache:masterfrom
alhudz:arrayutils-reverse-underflow
Jul 17, 2026
Merged

Fix ArrayUtils.reverse range underflow on Integer.MIN_VALUE end#1750
garydgregory merged 1 commit into
apache:masterfrom
alhudz:arrayutils-reverse-underflow

Conversation

@alhudz

@alhudz alhudz commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Repro: ArrayUtils.reverse(new int[]{1, 2, 3}, 0, Integer.MIN_VALUE).
Expected: no change, since the endIndexExclusive Javadoc documents an undervalue < start index as no change.
Actual: ArrayIndexOutOfBoundsException: Index 2147483647 out of bounds for length 3.
Cause: Math.min(array.length, endIndexExclusive) - 1 keeps Integer.MIN_VALUE and the - 1 underflows to Integer.MAX_VALUE, so the while (j > i) loop indexes past the array. All nine primitive and Object range overloads share the line.
Fix: clamp the end to zero before the decrement with the existing max0 helper, as the sibling subarray and shift methods already do. Every other negative end was already a no-op, so valid ranges are unchanged.

@garydgregory garydgregory changed the title fix ArrayUtils.reverse range underflow on Integer.MIN_VALUE end Fix ArrayUtils.reverse range underflow on Integer.MIN_VALUE end Jul 17, 2026
@garydgregory
garydgregory merged commit c99f2dd into apache:master Jul 17, 2026
21 checks passed
@garydgregory

Copy link
Copy Markdown
Member

Good find @alhudz, merged 🚀 Do you see any other similar index issue in this class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants