From 4040927d1750dd00611cc6465507dd0bc694a18f Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Tue, 9 Aug 2022 17:07:14 +0000 Subject: [PATCH] 8290047: (fs) FileSystem.getPathMatcher does not check for ":" at last index Reviewed-by: naoto, rriggs, alanb, lancea --- .../classes/java/nio/file/FileSystem.java | 5 +++-- .../jdk/internal/jrtfs/JrtFileSystem.java | 4 ++-- .../classes/sun/nio/fs/UnixFileSystem.java | 2 +- .../classes/sun/nio/fs/WindowsFileSystem.java | 4 ++-- .../classes/jdk/nio/zipfs/ZipFileSystem.java | 2 +- test/jdk/java/nio/file/PathMatcher/Basic.java | 12 +++++++++-- test/jdk/jdk/internal/jrtfs/Basic.java | 17 ++++++++++++++- test/jdk/jdk/nio/zipfs/Basic.java | 21 +++++++++++++++---- 8 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/java.base/share/classes/java/nio/file/FileSystem.java b/src/java.base/share/classes/java/nio/file/FileSystem.java index ab657718751..01b6dcc6992 100644 --- a/src/java.base/share/classes/java/nio/file/FileSystem.java +++ b/src/java.base/share/classes/java/nio/file/FileSystem.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -304,7 +304,8 @@ public abstract class FileSystem *
      * syntax:pattern
      * 
- * where {@code ':'} stands for itself. + * where syntax is the non-empty name of the syntax, pattern + * is a possibly-empty pattern string, and {@code ':'} stands for itself. * *

A {@code FileSystem} implementation supports the "{@code glob}" and * "{@code regex}" syntaxes, and may support others. The value of the syntax diff --git a/src/java.base/share/classes/jdk/internal/jrtfs/JrtFileSystem.java b/src/java.base/share/classes/jdk/internal/jrtfs/JrtFileSystem.java index ede008f4c00..9a8d9d22dfa 100644 --- a/src/java.base/share/classes/jdk/internal/jrtfs/JrtFileSystem.java +++ b/src/java.base/share/classes/jdk/internal/jrtfs/JrtFileSystem.java @@ -175,8 +175,8 @@ class JrtFileSystem extends FileSystem { @Override public PathMatcher getPathMatcher(String syntaxAndInput) { int pos = syntaxAndInput.indexOf(':'); - if (pos <= 0 || pos == syntaxAndInput.length()) { - throw new IllegalArgumentException("pos is " + pos); + if (pos <= 0) { + throw new IllegalArgumentException(); } String syntax = syntaxAndInput.substring(0, pos); String input = syntaxAndInput.substring(pos + 1); diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java b/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java index 5c4cc557dc0..0f5aec00c62 100644 --- a/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java +++ b/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java @@ -281,7 +281,7 @@ abstract class UnixFileSystem @Override public PathMatcher getPathMatcher(String syntaxAndInput) { int pos = syntaxAndInput.indexOf(':'); - if (pos <= 0 || pos == syntaxAndInput.length()) + if (pos <= 0) throw new IllegalArgumentException(); String syntax = syntaxAndInput.substring(0, pos); String input = syntaxAndInput.substring(pos+1); diff --git a/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystem.java b/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystem.java index 967c0da0fac..ecde6af3c25 100644 --- a/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystem.java +++ b/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystem.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -261,7 +261,7 @@ class WindowsFileSystem @Override public PathMatcher getPathMatcher(String syntaxAndInput) { int pos = syntaxAndInput.indexOf(':'); - if (pos <= 0 || pos == syntaxAndInput.length()) + if (pos <= 0) throw new IllegalArgumentException(); String syntax = syntaxAndInput.substring(0, pos); String input = syntaxAndInput.substring(pos+1); diff --git a/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java b/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java index 5adbd80f727..72f391d91c2 100644 --- a/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java +++ b/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java @@ -441,7 +441,7 @@ class ZipFileSystem extends FileSystem { @Override public PathMatcher getPathMatcher(String syntaxAndInput) { int pos = syntaxAndInput.indexOf(':'); - if (pos <= 0 || pos == syntaxAndInput.length()) { + if (pos <= 0) { throw new IllegalArgumentException(); } String syntax = syntaxAndInput.substring(0, pos); diff --git a/test/jdk/java/nio/file/PathMatcher/Basic.java b/test/jdk/java/nio/file/PathMatcher/Basic.java index 7440a54b87e..6a32ef0bdad 100644 --- a/test/jdk/java/nio/file/PathMatcher/Basic.java +++ b/test/jdk/java/nio/file/PathMatcher/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,7 +22,7 @@ */ /* @test - * @bug 4313887 6866397 8073445 + * @bug 4313887 6866397 8073445 8290047 * @summary Unit test for java.nio.file.PathMatcher */ @@ -84,6 +84,7 @@ public class Basic { public static void main(String[] args) { // basic + assertMatch("", ""); assertMatch("foo.html", "foo.html"); assertNotMatch("foo.html", "foo.htm"); assertNotMatch("foo.html", "bar.html"); @@ -137,6 +138,13 @@ public class Basic { assertBadPattern("foo.html", "*{class,java"); // missing } assertBadPattern("foo.html", "*.{class,{.java}}"); // nested group assertBadPattern("foo.html", "*.html\\"); // nothing to escape + try { + FileSystems.getDefault().getPathMatcher(":glob"); + System.err.println("No IllegalArgumentException for \":glob\""); + failures++; + } catch (IllegalArgumentException iae) { + System.out.println("IllegalArgumentException for \":glob\" OKAY"); + } // platform specific if (System.getProperty("os.name").startsWith("Windows")) { diff --git a/test/jdk/jdk/internal/jrtfs/Basic.java b/test/jdk/jdk/internal/jrtfs/Basic.java index cb2f20f78c4..2e2ca819a67 100644 --- a/test/jdk/jdk/internal/jrtfs/Basic.java +++ b/test/jdk/jdk/internal/jrtfs/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,6 +23,7 @@ /* * @test + * @bug 8141521 8216553 8266291 8290047 * @summary Basic test of jrt file system provider * @run testng Basic */ @@ -774,5 +775,19 @@ public class Basic { Files.exists(m); assertTrue(wasDirectory == Files.isDirectory(p)); } + + @DataProvider(name = "badSyntaxAndPattern") + private Object[][] badSyntaxAndPattern() { + return new Object[][] { + { ":glob"}, + }; + } + + @Test(dataProvider = "badSyntaxAndPattern", + expectedExceptions = IllegalArgumentException.class) + public void badSyntaxAndPatternTest(String syntaxAndPattern) { + FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/")); + PathMatcher pm = fs.getPathMatcher(syntaxAndPattern); + } } diff --git a/test/jdk/jdk/nio/zipfs/Basic.java b/test/jdk/jdk/nio/zipfs/Basic.java index e275e546e59..3d68d7f7cca 100644 --- a/test/jdk/jdk/nio/zipfs/Basic.java +++ b/test/jdk/jdk/nio/zipfs/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,7 +29,7 @@ import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; +import java.nio.file.PathMatcher; import java.nio.file.ProviderMismatchException; import java.nio.file.SimpleFileVisitor; import java.nio.file.StandardCopyOption; @@ -42,7 +42,7 @@ import java.util.Map; import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; /** * @test - * @bug 8038500 8040059 8150366 8150496 8147539 + * @bug 8038500 8040059 8150366 8150496 8147539 8290047 * @summary Basic test for zip provider * * @modules jdk.zipfs @@ -89,7 +89,7 @@ public class Basic { // Test: copy file from zip file to current (scratch) directory Path source = fs.getPath("/META-INF/services/java.nio.file.spi.FileSystemProvider"); if (Files.exists(source)) { - Path target = Paths.get(source.getFileName().toString()); + Path target = Path.of(source.getFileName().toString()); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); try { long s1 = Files.readAttributes(source, BasicFileAttributes.class).size(); @@ -113,6 +113,19 @@ public class Basic { throw new RuntimeException("watch service is not supported"); } catch (ProviderMismatchException x) { } + // Test: IllegalArgumentException + try { + PathMatcher pm = fs.getPathMatcher(":glob"); + throw new RuntimeException("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException iae) { + } + try { + PathMatcher pm = fs.getPathMatcher("glob:"); + } catch (IllegalArgumentException iae) { + iae.printStackTrace(); + throw new RuntimeException("Unexpected IllegalArgumentException"); + } + // Test: ClosedFileSystemException fs.close(); if (fs.isOpen())