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

String.format` in JDK 11 Behaves Incorrectly When Using a Width Exceeding IntegInteger.MAX_VALUE

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      When using the `String.format` method in JDK 11, if the width specified in the format specifier exceeds `Integer.MAX_VALUE` (i.e., 2147483647), an `IllegalFormatWidthException` is expected to be thrown. This behavior is documented in the official document ([Source](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html)):

      > "If the format specifier contains a width or precision with an invalid value or which is otherwise unsupported, an `IllegalFormatWidthException` or `IllegalFormatPrecisionException` will be thrown."

      However, in JDK 11, this exception is not thrown when the width exceeds `Integer.MAX_VALUE`. Instead, the error is silently ignored due to the `assert(false)` statement in the `width` method ([Source](https://github.com/openjdk/jdk/blob/jdk-11%2B25/src/java.base/share/classes/java/util/Formatter.java#L2814)). As a result, the method returns an incorrect value of `-1`, leading to erroneous outputs.






      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Version Information: JDK Version: OpenJDK 11.0.25
      Code Example:
      public class TestFormat {
          public static void main(String[] args) {
              String formatted = String.format("%2147483648d", 123);
              System.out.println("Formatted Value: " + formatted);
          }
      }

      Compilation and Execution Commands:
      javac TestFormat.java
      java TestFormat


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      An `IllegalFormatWidthException` should be thrown.
      ACTUAL -
       `Formatted Value: 123`

      FREQUENCY : always


            rkale Raghu Kale
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: