Skip to content

Commit 879a9f2

Browse files
authored
fix(core): http request timeout overflow (#785)
1 parent ae3f0a8 commit 879a9f2

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

algoliasearch-core/src/main/java/com/algolia/search/models/HttpRequest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ public boolean canCompress() {
111111
}
112112

113113
public void incrementTimeout(int retryCount) {
114-
this.timeout *= (retryCount + 1);
114+
// check for overflow
115+
if (timeout > 0 && retryCount > Integer.MAX_VALUE / timeout - 1) {
116+
timeout = Integer.MAX_VALUE;
117+
} else {
118+
timeout *= (retryCount + 1);
119+
}
115120
}
116121

117122
public Supplier<InputStream> getBodySupplier() {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.algolia.search;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.algolia.search.models.HttpMethod;
6+
import com.algolia.search.models.HttpRequest;
7+
import java.util.Collections;
8+
import org.junit.jupiter.api.Test;
9+
10+
class HttpRequestTest {
11+
12+
@Test
13+
void testIncrementTimeout() {
14+
HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, "/", Collections.emptyMap(), 1);
15+
16+
// Test normal case
17+
httpRequest.incrementTimeout(1);
18+
assertEquals(2, httpRequest.getTimeout()); // 1*(1+1) = 2
19+
20+
// Test large value
21+
httpRequest.incrementTimeout(Integer.MAX_VALUE);
22+
assertEquals(
23+
Integer.MAX_VALUE,
24+
httpRequest.getTimeout()); // Should have caused overflow and been set to MAX_VALUE
25+
26+
// Test timeout already at max value
27+
httpRequest.incrementTimeout(1);
28+
assertEquals(Integer.MAX_VALUE, httpRequest.getTimeout()); // Should not change timeout
29+
}
30+
}

0 commit comments

Comments
 (0)