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

DecimalFormat.setGroupingSize(int) allows setting negative grouping size

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 14
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b19
    • Verification:
      Verified

      Description

      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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              naoto Naoto Sato
              Reporter:
              nishjain Nishit Jain
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: