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

MessageFormat.setFormat() should check negative values

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 26
    • core-libs
    • None
    • Cause Known
    • generic
    • generic

      The method in the title checks for input indices larger than the internal format array length and throws a new AIOOBE if so. The method does not check negative values however which leads to the default AIOOBE being thrown for that case which causes a confusing error message.

      For example,

      jshell> var fmt = new MessageFormat("The date was {0,date,full}");
      fmt ==> java.text.MessageFormat@98462a7a

      jshell> fmt.setFormat(-1, new DecimalFormat())
      | Exception java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10
      | at MessageFormat.setFormat (MessageFormat.java:708)
      | at (#11:1)

      While the MessageFormat is only composed of a singular format/argument, the error message alludes to there being 10 formats/arguments. The code should be updated to throw a new AIOOBE as to not give off the misleading error.

      This fix should also take the opportunity to include any cleanup required in similar methods.

            jlu Justin Lu
            jlu Justin Lu
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: