Skip to content

Fix primitive array rotations at Integer.MIN_VALUE - #8807

Closed
wozhenbang2004 wants to merge 17 commits into
google:masterfrom
wozhenbang2004:fix/primitive-rotate-min-distance
Closed

wozhenbang2004 wants to merge 17 commits into
google:masterfrom
wozhenbang2004:fix/primitive-rotate-min-distance

Conversation

@wozhenbang2004

Copy link
Copy Markdown
Contributor

The primitive array rotate methods negate the distance before reducing it modulo the range length. Negating Integer.MIN_VALUE overflows, so rotating {1, 2, 3} by that distance produces {2, 3, 1} instead of {3, 1, 2}, unlike the documented Collections.rotate equivalent.

Reduce the distance modulo the range length before negating it in all eight primitive implementations. Add regression coverage for both whole-array and indexed rotations in each existing test class. No public API changes.

Validation:

  • An isolated OpenJDK 17 probe using the current source rotate/reverse methods and equivalent precondition checks compared 65,280 cases with Collections.rotate: eight primitive types, lengths 0–15, every subrange, and ten distances including Integer.MIN_VALUE, its next value, and Integer.MAX_VALUE. The original code had 2,728 mismatches; the fix had none.
  • All sixteen changed files were read back and checked against the intended changes.
  • The complete Guava Maven/JUnit suite has not been run locally. GitHub Actions validation is pending.

@cpovirk cpovirk self-assigned this Oct 30, 2026
@cpovirk cpovirk added type=defect Bug, not working as expected package=primitives P2 labels Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 package=primitives type=defect Bug, not working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants