Skip to content

Commit 4040927

Browse files
author
Brian Burkhalter
committed
8290047: (fs) FileSystem.getPathMatcher does not check for ":" at last index
Reviewed-by: naoto, rriggs, alanb, lancea
1 parent 8d0d9ea commit 4040927

File tree

8 files changed

+52
-15
lines changed

8 files changed

+52
-15
lines changed

src/java.base/share/classes/java/nio/file/FileSystem.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -304,7 +304,8 @@ protected FileSystem() {
304304
* <blockquote><pre>
305305
* <i>syntax</i><b>:</b><i>pattern</i>
306306
* </pre></blockquote>
307-
* where {@code ':'} stands for itself.
307+
* where <i>syntax</i> is the non-empty name of the syntax, <i>pattern</i>
308+
* is a possibly-empty pattern string, and {@code ':'} stands for itself.
308309
*
309310
* <p> A {@code FileSystem} implementation supports the "{@code glob}" and
310311
* "{@code regex}" syntaxes, and may support others. The value of the syntax

src/java.base/share/classes/jdk/internal/jrtfs/JrtFileSystem.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ public final String getSeparator() {
175175
@Override
176176
public PathMatcher getPathMatcher(String syntaxAndInput) {
177177
int pos = syntaxAndInput.indexOf(':');
178-
if (pos <= 0 || pos == syntaxAndInput.length()) {
179-
throw new IllegalArgumentException("pos is " + pos);
178+
if (pos <= 0) {
179+
throw new IllegalArgumentException();
180180
}
181181
String syntax = syntaxAndInput.substring(0, pos);
182182
String input = syntaxAndInput.substring(pos + 1);

src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public final Path getPath(String first, String... more) {
281281
@Override
282282
public PathMatcher getPathMatcher(String syntaxAndInput) {
283283
int pos = syntaxAndInput.indexOf(':');
284-
if (pos <= 0 || pos == syntaxAndInput.length())
284+
if (pos <= 0)
285285
throw new IllegalArgumentException();
286286
String syntax = syntaxAndInput.substring(0, pos);
287287
String input = syntaxAndInput.substring(pos+1);

src/java.base/windows/classes/sun/nio/fs/WindowsFileSystem.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -261,7 +261,7 @@ public GroupPrincipal lookupPrincipalByGroupName(String group)
261261
@Override
262262
public PathMatcher getPathMatcher(String syntaxAndInput) {
263263
int pos = syntaxAndInput.indexOf(':');
264-
if (pos <= 0 || pos == syntaxAndInput.length())
264+
if (pos <= 0)
265265
throw new IllegalArgumentException();
266266
String syntax = syntaxAndInput.substring(0, pos);
267267
String input = syntaxAndInput.substring(pos+1);

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ Path getZipFile() {
441441
@Override
442442
public PathMatcher getPathMatcher(String syntaxAndInput) {
443443
int pos = syntaxAndInput.indexOf(':');
444-
if (pos <= 0 || pos == syntaxAndInput.length()) {
444+
if (pos <= 0) {
445445
throw new IllegalArgumentException();
446446
}
447447
String syntax = syntaxAndInput.substring(0, pos);

test/jdk/java/nio/file/PathMatcher/Basic.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
2222
*/
2323

2424
/* @test
25-
* @bug 4313887 6866397 8073445
25+
* @bug 4313887 6866397 8073445 8290047
2626
* @summary Unit test for java.nio.file.PathMatcher
2727
*/
2828

@@ -84,6 +84,7 @@ static void assertRegExMatch(String path, String pattern) {
8484

8585
public static void main(String[] args) {
8686
// basic
87+
assertMatch("", "");
8788
assertMatch("foo.html", "foo.html");
8889
assertNotMatch("foo.html", "foo.htm");
8990
assertNotMatch("foo.html", "bar.html");
@@ -137,6 +138,13 @@ public static void main(String[] args) {
137138
assertBadPattern("foo.html", "*{class,java"); // missing }
138139
assertBadPattern("foo.html", "*.{class,{.java}}"); // nested group
139140
assertBadPattern("foo.html", "*.html\\"); // nothing to escape
141+
try {
142+
FileSystems.getDefault().getPathMatcher(":glob");
143+
System.err.println("No IllegalArgumentException for \":glob\"");
144+
failures++;
145+
} catch (IllegalArgumentException iae) {
146+
System.out.println("IllegalArgumentException for \":glob\" OKAY");
147+
}
140148

141149
// platform specific
142150
if (System.getProperty("os.name").startsWith("Windows")) {

test/jdk/jdk/internal/jrtfs/Basic.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
2323

2424
/*
2525
* @test
26+
* @bug 8141521 8216553 8266291 8290047
2627
* @summary Basic test of jrt file system provider
2728
* @run testng Basic
2829
*/
@@ -774,5 +775,19 @@ public void fileExistsCallBreaksFileSystem() throws Exception {
774775
Files.exists(m);
775776
assertTrue(wasDirectory == Files.isDirectory(p));
776777
}
778+
779+
@DataProvider(name = "badSyntaxAndPattern")
780+
private Object[][] badSyntaxAndPattern() {
781+
return new Object[][] {
782+
{ ":glob"},
783+
};
784+
}
785+
786+
@Test(dataProvider = "badSyntaxAndPattern",
787+
expectedExceptions = IllegalArgumentException.class)
788+
public void badSyntaxAndPatternTest(String syntaxAndPattern) {
789+
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
790+
PathMatcher pm = fs.getPathMatcher(syntaxAndPattern);
791+
}
777792
}
778793

test/jdk/jdk/nio/zipfs/Basic.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,7 +29,7 @@
2929
import java.nio.file.FileVisitResult;
3030
import java.nio.file.Files;
3131
import java.nio.file.Path;
32-
import java.nio.file.Paths;
32+
import java.nio.file.PathMatcher;
3333
import java.nio.file.ProviderMismatchException;
3434
import java.nio.file.SimpleFileVisitor;
3535
import java.nio.file.StandardCopyOption;
@@ -42,7 +42,7 @@
4242
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
4343
/**
4444
* @test
45-
* @bug 8038500 8040059 8150366 8150496 8147539
45+
* @bug 8038500 8040059 8150366 8150496 8147539 8290047
4646
* @summary Basic test for zip provider
4747
*
4848
* @modules jdk.zipfs
@@ -89,7 +89,7 @@ public static void main(String[] args) throws Exception {
8989
// Test: copy file from zip file to current (scratch) directory
9090
Path source = fs.getPath("/META-INF/services/java.nio.file.spi.FileSystemProvider");
9191
if (Files.exists(source)) {
92-
Path target = Paths.get(source.getFileName().toString());
92+
Path target = Path.of(source.getFileName().toString());
9393
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
9494
try {
9595
long s1 = Files.readAttributes(source, BasicFileAttributes.class).size();
@@ -113,6 +113,19 @@ public static void main(String[] args) throws Exception {
113113
throw new RuntimeException("watch service is not supported");
114114
} catch (ProviderMismatchException x) { }
115115

116+
// Test: IllegalArgumentException
117+
try {
118+
PathMatcher pm = fs.getPathMatcher(":glob");
119+
throw new RuntimeException("IllegalArgumentException not thrown");
120+
} catch (IllegalArgumentException iae) {
121+
}
122+
try {
123+
PathMatcher pm = fs.getPathMatcher("glob:");
124+
} catch (IllegalArgumentException iae) {
125+
iae.printStackTrace();
126+
throw new RuntimeException("Unexpected IllegalArgumentException");
127+
}
128+
116129
// Test: ClosedFileSystemException
117130
fs.close();
118131
if (fs.isOpen())

0 commit comments

Comments
 (0)