changeset 2313:00abf8c232be

6925932: (file) Path.endsWith can throw ArrayIndexOutOfBoundsException (unx) Reviewed-by: chegar
author alanb
date Tue, 23 Feb 2010 17:58:30 +0000
parents 38fbb2353a6a
children be5db597f3be
files src/solaris/classes/sun/nio/fs/UnixPath.java test/java/nio/file/Path/PathOps.java
diffstat 2 files changed, 31 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/classes/sun/nio/fs/UnixPath.java	Tue Feb 23 17:56:55 2010 +0000
+++ b/src/solaris/classes/sun/nio/fs/UnixPath.java	Tue Feb 23 17:58:30 2010 +0000
@@ -624,8 +624,11 @@
     public boolean endsWith(Path other) {
         UnixPath that = checkPath(other);
 
+        int thisLen = path.length;
+        int thatLen = that.path.length;
+
         // other path is longer
-        if (that.path.length > path.length)
+        if (thatLen > thisLen)
             return false;
 
         // other path is absolute so this path must be absolute
@@ -643,10 +646,10 @@
             if (thatOffsetCount == thisOffsetCount) {
                 if (thisOffsetCount == 0)
                     return true;
-                int expectedLen = path.length;
+                int expectedLen = thisLen;
                 if (this.isAbsolute() && !that.isAbsolute())
                     expectedLen--;
-                if (that.path.length != expectedLen)
+                if (thatLen != expectedLen)
                     return false;
             } else {
                 // this path has more elements so given path must be relative
@@ -658,7 +661,9 @@
         // compare bytes
         int thisPos = offsets[thisOffsetCount - thatOffsetCount];
         int thatPos = that.offsets[0];
-        while (thatPos < that.path.length) {
+        if ((thatLen - thatPos) != (thisLen - thisPos))
+            return false;
+        while (thatPos < thatLen) {
             if (this.path[thisPos++] != that.path[thatPos++])
                 return false;
         }
--- a/test/java/nio/file/Path/PathOps.java	Tue Feb 23 17:56:55 2010 +0000
+++ b/test/java/nio/file/Path/PathOps.java	Tue Feb 23 17:58:30 2010 +0000
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 4313887 6838333
+ * @bug 4313887 6838333 6925932
  * @summary Unit test for java.nio.file.Path path operations
  */
 
@@ -614,17 +614,34 @@
         test("/foo")
             .ends("foo")
             .ends("/foo")
-            .notEnds("/");
+            .notEnds("fool");
         test("/foo/bar")
             .ends("bar")
             .ends("foo/bar")
             .ends("/foo/bar")
-            .notEnds("/bar");
+            .notEnds("ar")
+            .notEnds("barack")
+            .notEnds("/bar")
+            .notEnds("o/bar");
         test("foo")
-            .ends("foo");
+            .ends("foo")
+            .notEnds("oo")
+            .notEnds("oola");
         test("foo/bar")
             .ends("bar")
-            .ends("foo/bar");
+            .ends("foo/bar")
+            .notEnds("r")
+            .notEnds("barmaid")
+            .notEnds("/bar");
+        test("foo/bar/gus")
+            .ends("gus")
+            .ends("bar/gus")
+            .ends("foo/bar/gus")
+            .notEnds("g")
+            .notEnds("/gus")
+            .notEnds("r/gus")
+            .notEnds("barack/gus")
+            .notEnds("bar/gust");
 
         // elements
         test("a/b/c")