Skip to content

Commit 51a4d09

Browse files
add check and test to prevent overflowing maximum integer before passing to Netty's configuration for connection timeout
1 parent 280afc6 commit 51a4d09

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncIggyTcpClientBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,16 @@ private void validateConnectionPoolSize() {
259259
}
260260

261261
private void validateConnectionTimeout() {
262-
if (connectionTimeout != null && (connectionTimeout.equals(Duration.ZERO) || connectionTimeout.isNegative())) {
262+
if (connectionTimeout == null) {
263+
return;
264+
}
265+
if (connectionTimeout.equals(Duration.ZERO) || connectionTimeout.isNegative()) {
263266
throw new IggyInvalidArgumentException("ConnectionTimeout Cannot be 0 or Negative");
264267
}
268+
if (connectionTimeout.toMillis() > ((long) Integer.MAX_VALUE)) {
269+
throw new IggyInvalidArgumentException(
270+
String.format("ConnectionTimeout Cannot be greater than %d", Integer.MAX_VALUE));
271+
}
265272
}
266273

267274
private void validateAcquireTimeout() {

foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/AsyncIggyTcpClientBuilderTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ void shouldThrowExceptionForNegativeConnectionTimeout() {
183183
assertThatThrownBy(builder::build).isInstanceOf(IggyInvalidArgumentException.class);
184184
}
185185

186+
@Test
187+
void shouldThrowExceptionForConnectionTimeoutInMillisecondsGreaterThanMaximumInteger() {
188+
// Given: Builder with connection timeout in milliseconds greater than maximum integer
189+
AsyncIggyTcpClientBuilder builder =
190+
AsyncIggyTcpClient.builder().connectionTimeout(Duration.ofMillis(((long) Integer.MAX_VALUE) + 1));
191+
192+
// When/Then: Building should throw IggyInvalidArgumentException
193+
assertThatThrownBy(builder::build).isInstanceOf(IggyInvalidArgumentException.class);
194+
}
195+
186196
@Test
187197
void shouldThrowExceptionForZeroAcquireTimeout() {
188198
// Given: Builder with 0 acquire timeout

0 commit comments

Comments
 (0)