String.indexOf(String, int) bug

Colin Stevens cstevens at eng.sun.com
Wed May 26 17:29:58 PDT 1999


String.indexOf(String str, int offset) crashes if str is a 0-length string,
since the implmentation always dereferences at least one character in the 
string.  A Q&D solution is to change String.indexOf(String, int) to check if 
"str" is length 0 (and then return 0) at the beginning.

Run this:

public class foo {
    public static void
    main(String[] args)
        throws Exception
    {
        String src = "abcdefghijklmnopqrstuvwxyz";
        int index = src.indexOf("");

        System.out.println(index);
    }
}

get this:

java.lang.ArrayIndexOutOfBoundsException
        at java.lang.String.indexOf(String.java:269)
        at java.lang.String.indexOf(String.java:257)
        at foo.main(foo.java:7)








More information about the kaffe mailing list