Skip to content

Commit b91134a

Browse files
davidkylesmalyshev
authored andcommitted
[ML] Protect against arithmetic overflow when using TimeValue.MAX_VALUE (#116749)
1 parent 5edda35 commit b91134a

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

muted-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ tests:
236236
- class: org.elasticsearch.snapshots.SnapshotShutdownIT
237237
method: testRestartNodeDuringSnapshot
238238
issue: https://github.com/elastic/elasticsearch/issues/116730
239-
- class: org.elasticsearch.xpack.inference.InferenceRestIT
240-
issue: https://github.com/elastic/elasticsearch/issues/116740
241239
- class: org.elasticsearch.action.search.SearchRequestTests
242240
method: testSerializationConstants
243241
issue: https://github.com/elastic/elasticsearch/issues/116752

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/time/RemainingTime.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ public interface RemainingTime extends Supplier<TimeValue> {
1818
* Create a {@link Supplier} that returns a decreasing {@link TimeValue} on each invocation, representing the amount of time until
1919
* the call times out. The timer starts when this method is called and counts down from remainingTime to 0.
2020
* currentTime should return the most up-to-date system time, for example Instant.now() or Clock.instant().
21+
* {@link TimeValue#MAX_VALUE} is a special case where the remaining time is always TimeValue.MAX_VALUE.
2122
*/
2223
static RemainingTime from(Supplier<Instant> currentTime, TimeValue remainingTime) {
24+
if (remainingTime.equals(TimeValue.MAX_VALUE)) {
25+
return () -> TimeValue.MAX_VALUE;
26+
}
27+
2328
var timeout = currentTime.get().plus(remainingTime.duration(), remainingTime.timeUnit().toChronoUnit());
2429
var maxRemainingTime = remainingTime.nanos();
2530
return () -> {

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/time/RemainingTimeTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public void testRemainingTimeMaxValue() {
3232
assertThat(remainingTime.get(), Matchers.equalTo(TimeValue.ZERO));
3333
}
3434

35+
public void testMaxTime() {
36+
var remainingTime = RemainingTime.from(Instant::now, TimeValue.MAX_VALUE);
37+
assertThat(remainingTime.get(), Matchers.equalTo(TimeValue.MAX_VALUE));
38+
}
39+
3540
// always add the first value, which is read when RemainingTime.from is called, then add the test values
3641
private Supplier<Instant> times(Instant... instants) {
3742
var startTime = Stream.of(Instant.now());

0 commit comments

Comments
 (0)