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

java.lang.String.regionMatches method is not compatible to specification

XMLWordPrintable

    • 1.1
    • sparc
    • solaris_2.5
    • Not verified



      Name: saf@russia Date: 08/07/96


      This bug was found by St.Petersburg Java SQE team (by Mikhail Gorshenev).

      The java.lang.String.regionMatches method does not work according to the Java
      language specification.

      The Java Language specification
      (Pre-Release Version 1.0, Draft 5.2 - July 3, 1996)
      says the following (please see item 20.12.13):

      "20.12.18 public boolean regionMatches(int toffset,
      String other, int ooffset, int len)
      throws NullPointerException

      A substring of this String object is compared to a substring of the
      argument other. The result is true if these substrings represent
      identical character sequences. The substring of this String
      object to be compared begins at index toffset and has length len.
      The substring of other to be compared begins at index ooffset and has
      length len. The result is false if and only if at least
      one of the following is true:

            toffset is negative.
            ooffset is negative.
            toffset+len is greater than the length of this String object.
            ooffset+len is greater than the length of the other argument.
            There is some nonnegative integer k less than len such that:
                  this.charAt(toffset+k) != other.charAt(ooffset+k)

      If other is null, then a NullPointerException is thrown."

      Method regionMatches() must return false if ooffset+len is greater than
      the length of the other argument, but if a private String field offset > 0
      it may incorrectly return true.
      (Offset is used when this string was created as substring of another
      string).
       
      Here is the minimized test demonstrating the bug:
          

      ----- java_lang_String_regionMatches.java -------------
      class java_lang_String_regionMatches {
        
        public static void main(String[] argv) {
            String s1 = "regionMatches() test !!!";
            String s2 = s1.substring(0,20);
            int toffset = 16;
            int ooffset = 16;
            int len = 7;
            /* s2 will be "regionMatches() test"
               s2.length() will be 20
             */
            boolean b=s1.regionMatches(toffset,s2,ooffset,len);
            /* ooffset + len > s2.length
               => b must be false
             */
            if (!b)
              System.out.println("Test passed: false is returned");
            else // but will return true!
              System.out.println("Test failed: true is returned");
        }
      }

      ----- The output of the test: -------------------------
      $JAVA java_lang_String_regionMatches
      Test failed: true is returned
      -------------------------------------------------------
      ======================================================================

            jrose John Rose
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: