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

Andrey Turbanov turbanoff at gmail.com
Fri Apr 8 06:27:45 UTC 2022


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'.


Andrey Turbanov


More information about the core-libs-dev mailing list