Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7963

TimeDuration fields can overflow int resulting in negative duration

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.4.6
    • None
    • None

    Description

      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. When the duration still remains positive at the end (e.g. being 3 days, -20 hours type of duration), it can be very hard to spot the error.

      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 TimeDuration.plus() 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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            sustacek@gmail.com Josef Sustacek
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: