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

java.text.DecimalFormat.applyPattern(String) allows illegal patterns

XMLWordPrintable

    • sparc
    • solaris_2.5



      Name: dfC67450 Date: 10/20/97



      The java.text.DecimalFormat.applyPattern(String pattern) allows to apply
      illegal patterns such as "#.#.#"
      Javadoc says:

       * The following shows the structure of the pattern.
       * pattern := subpattern{;subpattern}
       * subpattern := {prefix}integer{.fraction}{suffix}
       *
       * prefix := '\\u0000'..'\\uFFFD' - specialCharacters
       * suffix := '\\u0000'..'\\uFFFD' - specialCharacters
       * integer := '#'* '0'* '0'
       * fraction := '0'* '#'*
       *
       * Notation:
       * X* 0 or more instances of X
       * (X | Y) either X or Y.
       * X..Y any character from X up to Y, inclusive.
       * S - T characters in S, except those in T
       *
       * The first subpattern is for positive numbers. The second (optional)
       * subpattern is for negative numbers. (In both cases, ',' can occur
       * inside the integer portion--it is just too messy to indicate in BNF.)
       *
       * Here are the special characters used in the parts of the
       * subpattern, with notes on their usage.
       *
       * Symbol Meaning
       * 0 a digit
       * # a digit, zero shows as absent
       * . placeholder for decimal separator
       * , placeholder for grouping separator.
       * ; separates formats.
       * - default negative prefix.
       * % divide by 100 and show as percentage
       * X any other characters can be used in the prefix or suffix
       * ' used to quote special characters in a prefix or suffix.
       *
       * If there is no explicit negative subpattern, - is prefixed to the
       * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
       *
       * Illegal formats, such as "#.#.#" or mixing '_' and '*' in the
       * same format, will cause an ParseException to be thrown.
       * From that ParseException, you can find the place in the string
       * where the error occurred.
       *
       
      Here is the test demonstrating the bug:

      -----------------Test.java------------------------
      import java.text.*;

      public class Test2 {
          public static void main (String args[]){
              DecimalFormat df = new DecimalFormat();
              try {
                  df.applyPattern("#.#.#");
                  System.out.println("applyPattern(\"#.#.#\") doesn't throw IllegalArgumentException");
                  System.out.println("toPattern() returns \"" + df.toPattern() + "\"");
              } catch (IllegalArgumentException e) {
              }
          }
      }
      ---------Output from the test---------------------
      applyPattern("#.#.#") doesn't throw IllegalArgumentException
      toPattern() returns "#.##."
      --------------------------------------------------

      ======================================================================

      Name: dfC67450 Date: 10/23/97



      This method also allows to apply another illegal patterns such as
      "#0.0#0#0" and "#,#suffix#0.#"

      Yet another test demonstrating the bug
      -----------------TestA.java------------------------
      import java.text.*;

      public class TestA {
          public static void main (String args[]){
              DecimalFormat df = new DecimalFormat();
              try {
                  df.applyPattern("#0.0#0#0");
                  System.out.println("applyPattern(\"#0.0#0#0\") doesn't throw IllegalArgumentException");
                  System.out.println(" toPattern() returns \"" + df.toPattern() + "\"");
                  System.out.println();
              } catch (IllegalArgumentException e) {
              }
              try {
                  df.applyPattern("#,#suffix#0.#");
                  System.out.println("applyPattern(\"#,#suffix#0.#\") doesn't throw IllegalArgumentException");
                  System.out.println(" toPattern() returns \"" + df.toPattern() + "\"");
              } catch (IllegalArgumentException e) {
              }
          }
      }
      ---------Output from the test---------------------
      applyPattern("#0.0#0#0") doesn't throw IllegalArgumentException
        toPattern() returns "#0.000##"
       
      applyPattern("#,#suffix#0.#") doesn't throw IllegalArgumentException
        toPattern() returns "#,#.0##suffix."
      --------------------------------------------------

      ======================================================================

            duke J. Duke
            dfazunensunw Dmitri Fazunenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: