Always false condition in 'java.nio.file.FileSystem#getPathMatcher implementations

Alan Bateman Alan.Bateman at oracle.com
Fri Apr 8 07:07:29 UTC 2022


On 08/04/2022 07:27, Andrey Turbanov wrote:
> Hello.
> I found suspicious check in all 4 JDK implementations of
> java.nio.file.FileSystem#getPathMatcher
>
> https://github.com/openjdk/jdk/blob/8c1870521815a24fd12480e73450c2201542a442/src/java.base/share/classes/jdk/internal/jrtfs/JrtFileSystem.java#L178
> https://github.com/openjdk/jdk/blob/8c1870521815a24fd12480e73450c2201542a442/src/java.base/unix/classes/sun/nio/fs/UnixFileSystem.java#L285
> https://github.com/openjdk/jdk/blob/8c1870521815a24fd12480e73450c2201542a442/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystem.java#L264
> https://github.com/openjdk/jdk/blob/8c1870521815a24fd12480e73450c2201542a442/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java#L444
>
> public PathMatcher getPathMatcher(String syntaxAndInput) {
>      int pos = syntaxAndInput.indexOf(':');
>      if (pos <= 0 || pos == syntaxAndInput.length()) {
>          throw new IllegalArgumentException();
>      }
>
> Variable 'pos' is the result of the 'String.indexOf' method call. Then
> variable 'pos' is compared with 'length()' of the string with ==.
> But the maximum possible returned value of String.indexOf() is
> 'length()-1'. It means check 'pos == syntaxAndInput.length()' will
> always be 'false'.
It's benign and can be removed, I assume it was copied to the zipfs and 
jrtfs providers when they were initially created and just wasn't noticed.

-Alan


More information about the core-libs-dev mailing list