Skip to content

Commit 0115cd6

Browse files
lukasz-antoniakabsurdfarce
authored andcommitted
Prevent long overflow in SNI address resolution
patch by Lukasz Antoniak and Alexandre Dutra; reviewed by Bret McGuire
1 parent c43efd4 commit 0115cd6

File tree

1 file changed

+6
-3
lines changed
  • core/src/main/java/com/datastax/oss/driver/internal/core/metadata

1 file changed

+6
-3
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/metadata/SniEndPoint.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import java.util.Arrays;
2727
import java.util.Comparator;
2828
import java.util.Objects;
29-
import java.util.concurrent.atomic.AtomicLong;
29+
import java.util.concurrent.atomic.AtomicInteger;
3030

3131
public class SniEndPoint implements EndPoint {
32-
private static final AtomicLong OFFSET = new AtomicLong();
32+
private static final AtomicInteger OFFSET = new AtomicInteger();
3333

3434
private final InetSocketAddress proxyAddress;
3535
private final String serverName;
@@ -64,7 +64,10 @@ public InetSocketAddress resolve() {
6464
// The order of the returned address is unspecified. Sort by IP to make sure we get a true
6565
// round-robin
6666
Arrays.sort(aRecords, IP_COMPARATOR);
67-
int index = (aRecords.length == 1) ? 0 : (int) OFFSET.getAndIncrement() % aRecords.length;
67+
int index =
68+
(aRecords.length == 1)
69+
? 0
70+
: OFFSET.getAndUpdate(x -> x == Integer.MAX_VALUE ? 0 : x + 1) % aRecords.length;
6871
return new InetSocketAddress(aRecords[index], proxyAddress.getPort());
6972
} catch (UnknownHostException e) {
7073
throw new IllegalArgumentException(

0 commit comments

Comments
 (0)