Skip to content

Commit f15dbeb

Browse files
committed
fix rotate integer overflow for MIN_VALUE
1 parent 015b3e1 commit f15dbeb

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/main/java/io/github/jhspetersson/packrat/Packrat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,9 +1219,9 @@ public final class Packrat {
12191219
*/
12201220
@NonNull
12211221
public static <T> Gatherer<T, ?, T> rotate(int distance) {
1222-
if (distance < 0) {
1222+
if (distance < 0 && distance != Integer.MIN_VALUE) {
12231223
return new RotateLeftGatherer<>(-distance);
1224-
} else if (distance > 0) {
1224+
} else if (distance != 0) {
12251225
return new IntoListGatherer<>(list -> Collections.rotate(list, distance));
12261226
} else {
12271227
return new IdentityGatherer<>();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.jhspetersson.packrat;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
import java.util.stream.Stream;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class RotateOverflowBugTest {
11+
12+
@Test
13+
void rotateShouldHandleMinIntDistance() {
14+
var result = Stream.of(1, 2, 3).gather(Packrat.rotate(Integer.MIN_VALUE)).toList();
15+
assertEquals(3, result.size());
16+
}
17+
18+
@Test
19+
void rotateMinIntShouldProduceCorrectResult() {
20+
var result = Stream.of("a", "b", "c", "d").gather(Packrat.rotate(Integer.MIN_VALUE)).toList();
21+
assertEquals(List.of("a", "b", "c", "d"), result);
22+
}
23+
}

0 commit comments

Comments
 (0)