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

java.util.Formatter throws wrong exception for mismatched flags in %% conversion

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b10
    • Verification:
      Verified

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10 64-bits.

      Reproducible with all of:

      * openjdk version "1.8.0_275"
      * openjdk version "11.0.9" 2020-10-20
      * openjdk version "15.0.1" 2020-10-20
      * openjdk version "17-ea" 2021-09-14

      A DESCRIPTION OF THE PROBLEM :
      When using flags other than `-` with the `%%` conversion (for example with the string "%+%"), the JavaDoc at
      https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Formatter.html#dper
      specifies that a FormatFlagsConversionMismatchException should be thrown. However, an IllegalFormatFlagsException is thrown instead.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Using the provided test file below:

      $ javac -d bin test/Test.java
      $ java -cp bin test.Test


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No exception (each invocation should throw the exception that is specifically caught in the surrounding try/catch).
      ACTUAL -
      Exception in thread "main" java.util.IllegalFormatFlagsException: Flags = '+'
              at java.base/java.util.Formatter$FormatSpecifier.checkText(Formatter.java:3210)
              at java.base/java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2897)
              at java.base/java.util.Formatter.parse(Formatter.java:2729)
              at java.base/java.util.Formatter.format(Formatter.java:2671)
              at java.base/java.util.Formatter.format(Formatter.java:2625)
              at java.base/java.lang.String.format(String.java:3290)
              at test.Test.main(Test.java:26)

      (line 26 is the last call to String.format in the reproducing code)

      ---------- BEGIN SOURCE ----------
      package test;

      import java.util.*;

      public class Test {
        public static void main(String[] args) {
          try {
            // throws IllegalFormatFlagsException, as expected
            String.format("%+ %");
          } catch (IllegalFormatFlagsException e) {
            // ok
          }

          try {
            // throws IllegalFormatFlagsException, as expected as well
            // because %n has a special spec
            String.format("%+n");
          } catch (IllegalFormatFlagsException e) {
            // ok
          }

          try {
            // throws IllegalFormatFlagsException *as well*, although
            // the documentation says it should be
            // FormatFlagsConversionMismatchException
            String.format("%+%");
          } catch (FormatFlagsConversionMismatchException e) {
            // ok (unreachable)
          }
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Do not use incorrect format specifiers in `format`.

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              igraves Ian Graves
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: