[ 
https://issues.apache.org/jira/browse/LANG-1629?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17252264#comment-17252264
 ] 

Bruno P. Kinoshita commented on LANG-1629:
------------------------------------------

Hi!

Thanks for the detailed response [~helloJuhyun] . See [~aviprogrammer] 's reply 
above. I am still not able to reproduce the issue.

We want to confirm there's nothing wrong with 
`DurationFormatUtils.formatPeriod`, so I think we can remove your project code 
to create a really minimalistic way to reproduce the issue, if I understand 
your example right.

>if stime is 1608377400000, then an error(infinite-loops) occurs regardless of 
>whether etime is 1 or -1.
>I'm using DurationFormatUtils.formatPeriod in my project.

Here's what I am using, and I suspect [~aviprogrammer] is trying something 
similar.
{code:java}
    @Test
    public void testLang1629NegativeValues() {
        long stime = 1608377400000l;
        long etime = -62170189200000l;
        String formattedPeriod = DurationFormatUtils.formatPeriod(stime, etime, 
"HH:mm:ss");
        assertNotNull(formattedPeriod);
    } {code}
 

I tried that unit test, but if fails with the validation before any loop takes 
place.

I tried with values -1 and 1 for etime, but it didn't help.


>I'm trying to solve this problem, but as a junior developer, the code is 
>difficult for me.

That's all right. Slowly code like this should get easier for you. When 
reporting issues to projects (open source, or internally for $work), reporting
a detailed explanation is always helpful — as you did here. But it is equally 
important to make sure the developers are able to reproduce the issue.

Try the unit test above. Change the values to represent the parameter values 
that you think will trigger the loop. If you find a set of parameters, you can
share the values and we should be able to reproduce your issue. Please, try to 
avoid your project code if possible, this way we know that the issue is really
in Apache Commons Lang.

If you are not able to get the issue to happen with this test case, it is very 
likely the problem lies elsewhere in your code base. In that case,
you may want to sit with someone else that works on that project to code-review 
or rubber-duck-debug it. A debugger might help you identify what
is going on too.

Good luck, and keep us posted. If you are not able to reproduce it we should 
close the issue, but you are free to tell us later if you realize what was
happening.

Cheers
Bruno

> DurationFormatUtils.formatPeriod is infinite repetition when parameter is 
> negative number
> -----------------------------------------------------------------------------------------
>
>                 Key: LANG-1629
>                 URL: https://issues.apache.org/jira/browse/LANG-1629
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.time.*
>    Affects Versions: 3.11
>            Reporter: LeeJuHyun
>            Priority: Major
>             Fix For: Discussion
>
>
> if parameter number(*startMillis*, *endMillis*) is negative number then below 
> while loop is infinite repetition. (278 line ~)
> {code:java}
> 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);
> }{code}
>  
> How about putting in a validation to determine whether it's negative or 
> positive?
>  
> thank you :)
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to