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

String.indexOf(String,int) for the empty string case not specified

    Details

    • Subcomponent:
    • Resolved In Build:
      b04
    • OS:
      linux

      Description

      FULL PRODUCT VERSION :
      java version " 1.7.0_10 "
      Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
      Java HotSpot(TM) Server VM (build 23.6-b04, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux mima-nb 3.2.0-35-generic-pae #55-Ubuntu SMP Wed Dec 5 18:04:39 UTC 2012 i686 i686 i386 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      The contract for String.indexOf(String str, int fromIndex) as stated by javadoc is as follows:
         " The returned index is the smallest value k for which:
             k >= fromIndex && this.startsWith(str, k)
             
        If no such value of k exists, then -1 is returned. "

      Formally it will look like this:
        ( (k >= fromIndex) && this.startsWith(str, k) ) || (k == -1)
      which implicates
        (k >= fromIndex) || (k == -1)

      And here is where this contract is broken:
      given:
        s instanceof String
        fromIndex > s.length()
      when
        int k = s.indexOf( " " , fromIndex)
      then
        k == s.length()

      0 <= s.length() == k < fromIndex
        which implicates
      0 <= k < fromIndex
        which implicates
      !(k == -1) && !(k >= fromIndex)
        which implicates
      !(k >= fromIndex || k == -1)

      and contracts states exactly opposite:
        k >= fromIndex || k == -1

      This finishes the proof that the contract is broken in case presented above.

      The root cause of the bug is the first if in the following method:
          static int indexOf(char[] source, int sourceOffset, int sourceCount,
                  char[] target, int targetOffset, int targetCount,
                  int fromIndex) {
              if (fromIndex >= sourceCount) {
                  return (targetCount == 0 ? sourceCount : -1);
              }

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      String s = " bug " ;
      int fromIndex = s.length() + 1;
      int k = s.indexOf( " " , fromIndex);
      System.out.println ( " fromIndex= " + fromIndex + " , k= " + k);


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      // system output: fromIndex=4, k=-1
      ACTUAL -
      // system output: fromIndex=4, k=3

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class StringIndexOfContractBug {

          public static void main(String[] args) {
              String s = " bug " ;
              int fromIndex = s.length() + 1;
              int k = s.indexOf( " " , fromIndex);
              System.out.println ( " fromIndex= " + fromIndex + " , k= " + k);
          }

      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                bchristi Brent Christian
                Reporter:
                igerasim Ivan Gerasimov
              • Votes:
                1 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: