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)
======================================================================
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)
======================================================================