Skip to content

Prevent long overflow in SNI address resolution #2035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicInteger;

public class SniEndPoint implements EndPoint {
private static final AtomicLong OFFSET = new AtomicLong();
private static final AtomicInteger OFFSET = new AtomicInteger();

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