Obviously erroneous code in JapaneseImperialCalendar::actualMonthLength

Naoto Sato naoto.sato at oracle.com
Mon May 24 17:21:26 UTC 2021


Hi Tagir,

Thank you for reporting the issue. It is in fact already reported here:

https://bugs.openjdk.java.net/browse/JDK-8187649

This is a low priority (rarely occuring) bug, but since it is now 
offending static analyzers, I will take a second look.

Naoto

On 5/24/21 1:46 AM, Tagir Valeev wrote:
> Hello!
> 
> When testing our static analyzer, I've found obviously erroneous code
> in java.util.JapaneseImperialCalendar::actualMonthLength
> 
> int eraIndex = getTransitionEraIndex(jdate);
> if (eraIndex == -1) {
>      long transitionFixedDate = sinceFixedDates[eraIndex];
>      CalendarDate d = eras[eraIndex].getSinceDate();
>      if (transitionFixedDate <= cachedFixedDate) {
>         length -= d.getDayOfMonth() - 1;
>      } else {
>         length = d.getDayOfMonth() - 1;
>      }
> }
> 
> if this 'if' statement is visited then sinceFixedDates[eraIndex] will
> surely fail with ArrayIndexOutOfBoundsException. However, judging from
> the usages of this method, eraIndex is never -1 here, so the whole
> 'if' statement is a dead code. I'm not sure whether it should be
> removed, or the condition should be changed to 'eraIndex != -1' (and
> probably some unit-tests should be written to cover this). I have no
> expertise in this code so I'm not sure what would be a proper way to
> fix it. Please take a look.
> 
> With best regards,
> Tagir Valeev.
> 


More information about the core-libs-dev mailing list