Skip to content

Commit

Permalink
Avoid overflow when sorting missing last on epoch_millis datetime field
Browse files Browse the repository at this point in the history
Fixes #10253

Signed-off-by: Michael Froh <froh@amazon.com>
  • Loading branch information
msfroh committed Mar 14, 2024
1 parent 1717cc7 commit 2171127
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ public boolean isSupportedBy(TemporalAccessor temporal) {

@Override
public long getFrom(TemporalAccessor temporal) {
long instantSecondsInMillis = temporal.getLong(ChronoField.INSTANT_SECONDS) * 1_000;
long instantSeconds = temporal.getLong(ChronoField.INSTANT_SECONDS);
if (instantSeconds < Long.MIN_VALUE / 1000L || instantSeconds > Long.MAX_VALUE / 1000L) {
// Multiplying would yield integer overflow
return Long.MAX_VALUE;
}
long instantSecondsInMillis = instantSeconds * 1_000;
if (instantSecondsInMillis >= 0) {
if (temporal.isSupported(ChronoField.NANO_OF_SECOND)) {
return instantSecondsInMillis + (temporal.getLong(ChronoField.NANO_OF_SECOND) / 1_000_000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ public void testEpochMillisParser() {
assertThat(formatter.format(instant), is("-0.12345"));
assertThat(Instant.from(formatter.parse(formatter.format(instant))), is(instant));
}
{
Instant instant = Instant.ofEpochMilli(Long.MIN_VALUE);
assertThat(formatter.format(instant), is("-" + Long.MAX_VALUE)); // We actually truncate to Long.MAX_VALUE to avoid overflow
}
}

public void testInvalidEpochMilliParser() {
Expand Down

0 comments on commit 2171127

Please sign in to comment.