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

DecimalFormat::toPattern throws OutOfMemoryError when pattern is empty string

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 23
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      The compatibility risk is not zero, but incredibly low. While this potentially raises behavioral concerns, an empty String pattern has intention analogous to serving as a placeholder / "don't care". It is also no where specified in DecimalFormat what the default values for an empty String pattern are, so it is unlikely (while not impossible) that clients will be depending on the value of maximum fraction digits when formatting. It is also impossible for clients to be depending on the pattern returned, since such patterns would have caused OOME when toPattern was invoked.
      Show
      The compatibility risk is not zero, but incredibly low. While this potentially raises behavioral concerns, an empty String pattern has intention analogous to serving as a placeholder / "don't care". It is also no where specified in DecimalFormat what the default values for an empty String pattern are, so it is unlikely (while not impossible) that clients will be depending on the value of maximum fraction digits when formatting. It is also impossible for clients to be depending on the pattern returned, since such patterns would have caused OOME when toPattern was invoked.
    • Java API
    • JDK

      Summary

      Reduce the maximum fraction digits that an empty pattern DecimalFormat is initialized with.

      Problem

      An empty pattern DecimalFormat is initialized with Integer.MAX_VALUE digits for the maximum fraction digits. When toPattern() is invoked, a StringBuilder will append digits up to Integer.MAX_VALUE and internally double capacity until an OutOfMemoryError occurs. Additionally, DecimalFormat::toString utilizes toPattern(), which means that if toString() is invoked on an empty pattern DecimalFormat, an OutOfMemoryError occurs in this case as well.

      Solution

      Restrict the initial maximum fraction digits for an empty pattern from Integer.MAX_VALUE to the private constant variable, DecimalFormat.DOUBLE_FRACTION_DIGITS. This allows for empty pattern DecimalFormat's to no longer cause an OutOfMemoryError, as the value is reduced to 340. This value is a reasonable choice, serving as the upper fraction limit for a Java double.

      Specification

      N/A (Behavioral change only)

            jlu Justin Lu
            kganapureddy Krushnareddy Ganapureddy
            Naoto Sato
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: