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

java.text.StringCharacterIterator constructor allows large endIndexes

XMLWordPrintable

    • 1.1.6
    • sparc
    • solaris_2.5
    • Verified



        Name: mgC56079 Date: 09/11/97



        The java.text.StringCharacterIterator.StringCharacterIterator(String text,
                                                      int begin, int end, int pos)
        constructor does not check whether end > text.length().
        It checks only whether 0<=begin<=pos<=end.
        Should check for 0<=begin<=pos<=end<=text.length().

        As a result a StringCharacterIterator may be created which has an invalid
        state (the current() method will throw a StringIndexOutOfBoundsException).

        ------------------------- Here is the source --------------------------
        ------( No check whether end is larger than text.length() is done) ----
            public StringCharacterIterator(String text, int begin, int end, int pos) {
                if (text == null)
                    throw new NullPointerException();
                this.text = text;

        if (begin < 0 || begin > end)
        throw new IllegalArgumentException("Invalid substring range");

        if (pos < begin || pos > end)
        throw new IllegalArgumentException("Invalid position");

        this.begin = begin;
        this.end = end;
        this.pos = pos;
            }

        ---------Here is the test demonstrating the bug (SCITest2.java) ----------

        import java.text.StringCharacterIterator;

        public class SCITest2 {
          public static void main(String args[]) {
            StringCharacterIterator sci =
              new StringCharacterIterator("The string", 0, 100, 99);
            System.out.print("Current char: ");
            System.out.println(sci.current());
            
          }
        }

        ---------Output from the test---------------------
        % java SCITest2
        Current char: java.lang.StringIndexOutOfBoundsException: String index out of range: 99
        at java.lang.String.charAt(String.java)
        at java.text.StringCharacterIterator.current(StringCharacterIterator.java:174)
        at SCITest2.main(SCITest2.java:8)
        --------------------------------------------------

        ======================================================================

              nlindenbsunw Norbert Lindenberg (Inactive)
              mgorshen Mikhail Gorshenev (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: