-
Bug
-
Resolution: Fixed
-
P4
-
None
-
b19
-
Verified
The DecimalFormat.setGroupingSize(int) allows setting negative values, which may not be correct for a property like grouping size.
DecimalFormat fmt = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
fmt.setGroupingSize(-2);
System.out.println(fmt.getGroupingSize());
Instead of throwing exception, the above code snippet accepts -2 to be set for grouping size
Expected: Throw an IllegalArgumentException
Actual: Negative values are accepted.
Also, the specification of setGroupingSize() says that "The value passed in is converted to a byte, which may lose information."
so, setting a value outside "byte" boundary, wraps around e.g.
fmt.setGroupingSize(255); // makes the grouping size as -1
Instead of accepting all integer values, a range check should be applied before setting the grouping size field. e.g. values between 0-127
DecimalFormat fmt = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
fmt.setGroupingSize(-2);
System.out.println(fmt.getGroupingSize());
Instead of throwing exception, the above code snippet accepts -2 to be set for grouping size
Expected: Throw an IllegalArgumentException
Actual: Negative values are accepted.
Also, the specification of setGroupingSize() says that "The value passed in is converted to a byte, which may lose information."
so, setting a value outside "byte" boundary, wraps around e.g.
fmt.setGroupingSize(255); // makes the grouping size as -1
Instead of accepting all integer values, a range check should be applied before setting the grouping size field. e.g. values between 0-127
- csr for
-
JDK-8231851 DecimalFormat.setGroupingSize(int) allows setting negative grouping size
-
- Closed
-
- relates to
-
JDK-8231984 Clarify semantics of DecimalFormat.getGroupingSize(0)
-
- Resolved
-