groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Josef Sustacek (JIRA)" <>
Subject [jira] [Created] (GROOVY-7963) TimeDuration fields can overflow int resulting in negative duration
Date Mon, 10 Oct 2016 12:21:20 GMT
Josef Sustacek created GROOVY-7963:

             Summary: TimeDuration fields can overflow int resulting in negative duration
                 Key: GROOVY-7963
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 2.4.6
            Reporter: Josef Sustacek

When you accidentally or on purposes add ({{.plus()}}) two time durations where some unit
can no longer fit {{int}}, this unit will overflow in the new object. This can lead to negative
duration and / or incorrect calculations with this new duration.

Example code:
import groovy.time.TimeCategory

use (TimeCategory) {
    def duration = Integer.MAX_VALUE.milliseconds + 1.millisecond

    println duration

Result (groovy 2.4.6):
Result: -2147483.648 seconds

This is due to the fact that the BaseDuration has all units' fields implemented as {{int}}
and the simply adds the ints of each unit of the two durations together,
forming the new duration:

Expected: Either check this in all the {{.plus() }} methods and prevent such two durations
to be added together, of use {{long}} for the units's fields to make sure they cannot overflow
when two large {{int}}s are added together.

This message was sent by Atlassian JIRA

View raw message