-
Bug
-
Resolution: Fixed
-
P4
-
None
-
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) {
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) {