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

java.lang.String uses wrong variable in constructor

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.0
    • core-libs
    • merlin
    • generic
    • generic

      Name: mc57594 Date: 07/18/99

      In one of the constructors for java.lang.String, the wrong
      variable name is used when throwing an exception (the code
      uses count, should use length) -- see suggested code fix.

      This error could result in a StringIndexOutOfBoundsException
      with the wrong index value.

      ZZ
      TEST CASE
      public class Bug {
        public static void main(String args[]) {
              byte b[] = new byte[10];
              for(int i = 0; i < b.length; i++) b[i] = (byte)(64+i);
              String s = new String(b,6,6);
              }
        }

      This produces the error message
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
              at java.lang.Throwable.fillInStackTrace(Native Method)
              at java.lang.Throwable.<init>(Throwable.java:94)
              at java.lang.Exception.<init>(Exception.java:42)
              at java.lang.RuntimeException.<init>(RuntimeException.java:47)
              at java.lang.IndexOutOfBoundsException.<init>(IndexOutOfBoundsException.java:44)
              at java.lang.StringIndexOutOfBoundsException.<init>(StringIndexOutOfBoundsException.java:57)
              at java.lang.String.<init>(String.java:358)
              at java.lang.String.<init>(Compiled Code)
              at Bug.main(Compiled Code)

      To be consistent with the other constructors for String,
      it should report index 12.

      Of course, it is also questionable as to whether 12 is the right
      value to report as well. With an offset of 6 and a length of 6,
      the user is indicating that the string should be formed from
      elements 6...11 of the byte array. 12 isn't an index included
      by the user's parameters. So you might want to change it to be
      offset+length-1; but you would want to do so consistantly across
      all of the String constructors (and perhaps similar error
      messages elsewhere).
      (Review ID: 85735)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: