Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8176847

Calendar.checkDisplayNameParams() should check case of style == 3

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 10
    • None
    • core-libs
    • None
    • b11
    • Verified

      As reported by a colleague:

      java.util.Calendar.checkDisplayNameParams() checks a specified style against minStyle / maxStyle boundaries. Those boundaries are taken from the named constants:

      ALL_STYLES = 0
      SHORT = 1
      LONG = 2
      NARROW_FORMAT = 4

      but the method forgets to check for the value 3 which is in-between LONG and NARROW_FORMAT but doesn't correspond to a named style constant.

      This means that Calendar.getDisplayName() will not throw IllegalArgumentException when given a style parameter value of 3, even though that value is invalid and the method's documentation claims that such an exception is thrown "if {@code field} or {@code style} is invalid".

      The following patch (also attached as calendar.diff) to OpenJDK's latest jdk9 code makes the behavior more consistent with the documentation by throwing IllegalArgumentException in the case of style == 3:

      diff -r 09b92d3067a3 src/java.base/share/classes/java/util/Calendar.java
      --- a/src/java.base/share/classes/java/util/Calendar.java Mon Mar 13 10:24:16 2017 -0700
      +++ b/src/java.base/share/classes/java/util/Calendar.java Thu Mar 16 11:49:10 2017 +1100
      @@ -2216,7 +2216,7 @@
                                          Locale locale, int fieldMask) {
               int baseStyle = getBaseStyle(style); // Ignore the standalone mask
               if (field < 0 || field >= fields.length ||
      - baseStyle < minStyle || baseStyle > maxStyle) {
      + baseStyle < minStyle || baseStyle > maxStyle || baseStyle == 3) {
                   throw new IllegalArgumentException();
               }
               if (locale == null) {

            naoto Naoto Sato
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: