-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gax): prevent truncation/overflow when converting time values #3095
Changes from 1 commit
0fb8160
329c467
5014c4b
86d5855
cfa3b06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,44 @@ void testToThreetenTimeInstant_validInput_succeeds() { | |
jtInstant.toEpochMilli(), TimeConversionUtils.toThreetenInstant(jtInstant).toEpochMilli()); | ||
assertNull(TimeConversionUtils.toThreetenInstant(null)); | ||
} | ||
|
||
@Test | ||
void testToThreeteenInstant_bigInput_doesNotOverflow() { | ||
// defaults to MAX_SECONDS plus the max value of long for the nanos part | ||
java.time.Instant jtInstant = java.time.Instant.MAX; | ||
org.threeten.bp.Instant ttInstant = TimeConversionUtils.toThreetenInstant(jtInstant); | ||
assertEquals(jtInstant.getEpochSecond(), ttInstant.getEpochSecond()); | ||
assertEquals(jtInstant.getNano(), ttInstant.getNano()); | ||
} | ||
|
||
@Test | ||
void testToJavaTimeInstant_bigInput_doesNotOverflow() { | ||
// defaults to MAX_SECONDS plus the max value of long for the nanos part | ||
org.threeten.bp.Instant ttInstant = org.threeten.bp.Instant.MAX; | ||
java.time.Instant jtInstant = TimeConversionUtils.toJavaTimeInstant(ttInstant); | ||
assertEquals(jtInstant.getEpochSecond(), ttInstant.getEpochSecond()); | ||
assertEquals(jtInstant.getNano(), ttInstant.getNano()); | ||
} | ||
|
||
@Test | ||
void testToThreeteenDuration_bigInput_doesNotOverflow() { | ||
// we use the max long value for the seconds part and an arbitrary int for the nanos part, so we | ||
// can confirm | ||
// that both components are preserved | ||
diegomarquezp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
java.time.Duration jtDuration = java.time.Duration.ofSeconds(Long.MAX_VALUE, 123); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. qq, any specific reason for an arbitrary value of 123 and not Long/Integer.MAX_VALUE? Perhaps we use MAX_VALUE for consistency? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No specific reason. I modified this to use the max possible nano value without overflowing the duration |
||
org.threeten.bp.Duration ttDuration = TimeConversionUtils.toThreetenDuration(jtDuration); | ||
assertEquals(jtDuration.getSeconds(), ttDuration.getSeconds()); | ||
assertEquals(jtDuration.getNano(), ttDuration.getNano()); | ||
} | ||
|
||
@Test | ||
void testToJavaTimeDuration_bigInput_doesNotOverflow() { | ||
// we use the max long value for the seconds part and an arbitrary int for the nanos part, so we | ||
// can confirm | ||
// that both components are preserved | ||
diegomarquezp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
org.threeten.bp.Duration ttDuration = org.threeten.bp.Duration.ofSeconds(Long.MAX_VALUE, 123); | ||
java.time.Duration jtDuration = TimeConversionUtils.toJavaTimeDuration(ttDuration); | ||
assertEquals(jtDuration.getSeconds(), ttDuration.getSeconds()); | ||
assertEquals(jtDuration.getNano(), ttDuration.getNano()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too familiar with this. What do you mean by
plus the max value of long for nanos
?I see that the MAX is
1000000000-12-31T23:59:59.999999999Z
. Does this mean that this is essentially where jtInstant.getNano() and jtInstant.getEpochSecond() are both Interger.MAX_VALUE?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instants and Durations have two components: the seconds part and the nanos part, both being
long
forDuration
, andlong
for seconds +int
for nanos in Instant.Yes, but with
Long.MAX_VALUE
for the seconds part instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok gotcha. I couldn't make sense of where the overflow was originally coming from since MAX_VALUE itself shouldn't trigger an overflow. If I'm understanding this correctly, it was triggered from the old way of how we were converting.
The
toNanos()
method was limited to Long.MAX_VALUE which has a limit of 292 years. Since Instant.MAX_VALUE has Long.MAX_VALUE for both seconds and nanos, it wouldn't be able to fit intotoNanos()
into a Long.MAX_VALUE as adding all the seconds + nanos would easily overfill.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly.
Yes. I don't think we will ever deal with durations instants that last longer than a few hours but it's good to support the theoretical limits of these classes, even because a user may just try a max value and that may break the library.