Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1629

DurationFormatUtils.formatPeriod is infinite repetition when parameter is negative number

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 3.11
    • Discussion
    • lang.time.*
    • None

    Description

      if parameter number(startMillis, endMillis) is negative number then below while loop is infinite repetition. (278 line ~)

      public static String formatPeriod(final long startMillis, final long endMillis, final String format, final boolean padWithZeros,
              final TimeZone timezone) {
          Validate.isTrue(startMillis <= endMillis, "startMillis must not be greater than endMillis");
      
      
          // Used to optimise for differences under 28 days and
          // called formatDuration(millis, format); however this did not work
          // over leap years.
          // TODO: Compare performance to see if anything was lost by
          // losing this optimisation.
      
          final Token[] tokens = lexx(format); 
          // ...
          while (start.get(Calendar.YEAR) != target) {
          days += start.getActualMaximum(Calendar.DAY_OF_YEAR) - start.get(Calendar.DAY_OF_YEAR);
      
          // Not sure I grok why this is needed, but the brutal tests show it is
          if (start instanceof GregorianCalendar &&
                  start.get(Calendar.MONTH) == Calendar.FEBRUARY &&
                  start.get(Calendar.DAY_OF_MONTH) == 29) {
              days += 1;
          }
      
          start.add(Calendar.YEAR, 1);
      
          days += start.get(Calendar.DAY_OF_YEAR);
      }

       

      How about putting in a validation to determine whether it's negative or positive?

       

      thank you

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            helloJuhyun LeeJuHyun
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h