Skip to content

Commit fcecd00

Browse files
authored
Avoid overflow when sorting missing last on epoch_millis datetime field (#12676)
Fixes #10253 Signed-off-by: Michael Froh <froh@amazon.com>
1 parent 1717cc7 commit fcecd00

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
157157
- Fix `terms` query on `float` field when `doc_values` are turned off by reverting back to `FloatPoint` from `FloatField` ([#12499](https://github.com/opensearch-project/OpenSearch/pull/12499))
158158
- Fix get task API does not refresh resource stats ([#11531](https://github.com/opensearch-project/OpenSearch/pull/11531))
159159
- onShardResult and onShardFailure are executed on one shard causes opensearch jvm crashed ([#12158](https://github.com/opensearch-project/OpenSearch/pull/12158))
160+
- Avoid overflow when sorting missing last on `epoch_millis` datetime field ([#12676](https://github.com/opensearch-project/OpenSearch/pull/12676))
160161

161162
### Security
162163

server/src/main/java/org/opensearch/common/time/EpochTime.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ public boolean isSupportedBy(TemporalAccessor temporal) {
126126

127127
@Override
128128
public long getFrom(TemporalAccessor temporal) {
129-
long instantSecondsInMillis = temporal.getLong(ChronoField.INSTANT_SECONDS) * 1_000;
129+
long instantSeconds = temporal.getLong(ChronoField.INSTANT_SECONDS);
130+
if (instantSeconds < Long.MIN_VALUE / 1000L || instantSeconds > Long.MAX_VALUE / 1000L) {
131+
// Multiplying would yield integer overflow
132+
return Long.MAX_VALUE;
133+
}
134+
long instantSecondsInMillis = instantSeconds * 1_000;
130135
if (instantSecondsInMillis >= 0) {
131136
if (temporal.isSupported(ChronoField.NANO_OF_SECOND)) {
132137
return instantSecondsInMillis + (temporal.getLong(ChronoField.NANO_OF_SECOND) / 1_000_000);

server/src/test/java/org/opensearch/common/time/DateFormattersTests.java

+4
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ public void testEpochMillisParser() {
211211
assertThat(formatter.format(instant), is("-0.12345"));
212212
assertThat(Instant.from(formatter.parse(formatter.format(instant))), is(instant));
213213
}
214+
{
215+
Instant instant = Instant.ofEpochMilli(Long.MIN_VALUE);
216+
assertThat(formatter.format(instant), is("-" + Long.MAX_VALUE)); // We actually truncate to Long.MAX_VALUE to avoid overflow
217+
}
214218
}
215219

216220
public void testInvalidEpochMilliParser() {

0 commit comments

Comments
 (0)