You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Josef Sustacek (JIRA)" <ji...@apache.org> on 2016/10/10 12:21:20 UTC

[jira] [Created] (GROOVY-7963) TimeDuration fields can overflow int resulting in negative duration

Josef Sustacek created GROOVY-7963:
--------------------------------------

             Summary: TimeDuration fields can overflow int resulting in negative duration
                 Key: GROOVY-7963
                 URL: https://issues.apache.org/jira/browse/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:
{code}
import groovy.time.TimeCategory

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

    println duration
}
{code}

Result (groovy 2.4.6):
{code}
Result: -2147483.648 seconds
{code}

This is due to the fact that the BaseDuration has all units' fields implemented as {{int}} and the TimeDuration.plus() simply adds the ints of each unit of the two durations together, forming the new duration:
* https://github.com/groovy/groovy-core/blob/master/src/main/groovy/time/Duration.java#L42-L46


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
(v6.3.4#6332)