提交 9456a024 编写于 作者: A alanb

6925932: (file) Path.endsWith can throw ArrayIndexOutOfBoundsException (unx)

Reviewed-by: chegar
上级 f7799fb3
...@@ -624,8 +624,11 @@ class UnixPath ...@@ -624,8 +624,11 @@ class UnixPath
public boolean endsWith(Path other) { public boolean endsWith(Path other) {
UnixPath that = checkPath(other); UnixPath that = checkPath(other);
int thisLen = path.length;
int thatLen = that.path.length;
// other path is longer // other path is longer
if (that.path.length > path.length) if (thatLen > thisLen)
return false; return false;
// other path is absolute so this path must be absolute // other path is absolute so this path must be absolute
...@@ -643,10 +646,10 @@ class UnixPath ...@@ -643,10 +646,10 @@ class UnixPath
if (thatOffsetCount == thisOffsetCount) { if (thatOffsetCount == thisOffsetCount) {
if (thisOffsetCount == 0) if (thisOffsetCount == 0)
return true; return true;
int expectedLen = path.length; int expectedLen = thisLen;
if (this.isAbsolute() && !that.isAbsolute()) if (this.isAbsolute() && !that.isAbsolute())
expectedLen--; expectedLen--;
if (that.path.length != expectedLen) if (thatLen != expectedLen)
return false; return false;
} else { } else {
// this path has more elements so given path must be relative // this path has more elements so given path must be relative
...@@ -658,7 +661,9 @@ class UnixPath ...@@ -658,7 +661,9 @@ class UnixPath
// compare bytes // compare bytes
int thisPos = offsets[thisOffsetCount - thatOffsetCount]; int thisPos = offsets[thisOffsetCount - thatOffsetCount];
int thatPos = that.offsets[0]; 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++]) if (this.path[thisPos++] != that.path[thatPos++])
return false; return false;
} }
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
*/ */
/* @test /* @test
* @bug 4313887 6838333 * @bug 4313887 6838333 6925932
* @summary Unit test for java.nio.file.Path path operations * @summary Unit test for java.nio.file.Path path operations
*/ */
...@@ -614,17 +614,34 @@ public class PathOps { ...@@ -614,17 +614,34 @@ public class PathOps {
test("/foo") test("/foo")
.ends("foo") .ends("foo")
.ends("/foo") .ends("/foo")
.notEnds("/"); .notEnds("fool");
test("/foo/bar") test("/foo/bar")
.ends("bar") .ends("bar")
.ends("foo/bar") .ends("foo/bar")
.ends("/foo/bar") .ends("/foo/bar")
.notEnds("/bar"); .notEnds("ar")
.notEnds("barack")
.notEnds("/bar")
.notEnds("o/bar");
test("foo") test("foo")
.ends("foo"); .ends("foo")
.notEnds("oo")
.notEnds("oola");
test("foo/bar") test("foo/bar")
.ends("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 // elements
test("a/b/c") test("a/b/c")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册