From e41b2d529762c4e1ca163afba1bd826e75d3dfbc Mon Sep 17 00:00:00 2001 From: Paint_Ninja Date: Fri, 15 Mar 2024 22:08:55 +0000 Subject: [PATCH] Fix erroneous identity comparison in `UnionFileSystem#newDirStream` and remove redundant checks (#1) --- .../cpw/mods/niofs/union/UnionFileSystem.java | 17 ++++++----------- .../java/cpw/mods/niofs/union/UnionPath.java | 4 ++-- .../securemodules/SecureModuleClassLoader.java | 4 ++-- .../securemodules/SecureModuleFinder.java | 8 +------- 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/main/java/cpw/mods/niofs/union/UnionFileSystem.java b/src/main/java/cpw/mods/niofs/union/UnionFileSystem.java index 8364f28..dc7ec28 100644 --- a/src/main/java/cpw/mods/niofs/union/UnionFileSystem.java +++ b/src/main/java/cpw/mods/niofs/union/UnionFileSystem.java @@ -105,7 +105,6 @@ public synchronized Throwable fillInStackTrace() { } } private final UnionPath root = new UnionPath(this, "/"); - private final UnionPath notExistingPath = new UnionPath(this, "/SNOWMAN"); private final UnionFileSystemProvider provider; private final String key; private final List basepaths; @@ -254,7 +253,7 @@ private static boolean zipFsExists(UnionFileSystem ufs, Path path) { private Optional findFirstFiltered(final UnionPath path) { for (Path p : this.basepaths) { Path realPath = toRealPath(p, path); - if (realPath != notExistingPath && testFilter(realPath, p)) { + if (testFilter(realPath, p)) { if (realPath.getFileSystem() == FileSystems.getDefault()) { if (realPath.toFile().exists()) { return Optional.of(realPath); @@ -278,11 +277,9 @@ public A readAttributes(final UnionPath path, fi for (Path base : this.basepaths) { // We need to know the full path for the filter Path realPath = toRealPath(base, path); - if (realPath != notExistingPath) { - Optional fileAttributes = this.getFileAttributes(realPath); - if (fileAttributes.isPresent() && testFilter(realPath, base)) { - return (A) fileAttributes.get(); - } + Optional fileAttributes = this.getFileAttributes(realPath); + if (fileAttributes.isPresent() && testFilter(realPath, base)) { + return (A) fileAttributes.get(); } } throw new NoSuchFileException(path.toString()); @@ -346,11 +343,9 @@ public DirectoryStream newDirStream(final UnionPath path, final DirectoryS final var allpaths = new LinkedHashSet(); for (final var bp : basepaths) { final var dir = toRealPath(bp, path); - if (dir == notExistingPath) { - continue; - } else if (dir.getFileSystem() == FileSystems.getDefault() && !dir.toFile().exists()) { + if (dir.getFileSystem() == FileSystems.getDefault() && !dir.toFile().exists()) { continue; - } else if (dir.getFileSystem().provider().getScheme() == "jar" && !zipFsExists(this, dir)) { + } else if (dir.getFileSystem().provider().getScheme().equals("jar") && !zipFsExists(this, dir)) { continue; } else if (Files.notExists(dir)) { continue; diff --git a/src/main/java/cpw/mods/niofs/union/UnionPath.java b/src/main/java/cpw/mods/niofs/union/UnionPath.java index a891dbc..504ac98 100644 --- a/src/main/java/cpw/mods/niofs/union/UnionPath.java +++ b/src/main/java/cpw/mods/niofs/union/UnionPath.java @@ -231,13 +231,13 @@ public Path resolve(final Path other) { public Path relativize(final Path other) { if (other.getFileSystem()!=this.getFileSystem()) throw new IllegalArgumentException("Wrong filesystem"); if (other instanceof UnionPath p) { - if (this.absolute != p.absolute) { + //if (this.absolute != p.absolute) { // Should not be allowed but union fs relies on it // also there is no such concept of a current directory for union paths // meaning absolute and relative paths should have the same effect, // so we just allow this. //throw new IllegalArgumentException("Different types of path"); - } + //} var length = Math.min(this.pathParts.length, p.pathParts.length); int i = 0; while (i < length) { diff --git a/src/main/java/net/minecraftforge/securemodules/SecureModuleClassLoader.java b/src/main/java/net/minecraftforge/securemodules/SecureModuleClassLoader.java index aa8314b..efdf47a 100644 --- a/src/main/java/net/minecraftforge/securemodules/SecureModuleClassLoader.java +++ b/src/main/java/net/minecraftforge/securemodules/SecureModuleClassLoader.java @@ -311,7 +311,7 @@ public Enumeration getResources(String name) throws IOException { // TODO: [SM] If our parent is null we should look a the bootstrap classloader, but that requires calling super, which will duplicate resources return new Enumeration<>() { - private Enumeration> itr = Collections.enumeration(results); + private final Enumeration> itr = Collections.enumeration(results); private Enumeration current = findNext(); private Enumeration findNext() { @@ -598,7 +598,7 @@ private ModuleReader getModuleReader(ModuleReference reference) { }); } - private static ModuleReader NOOP_READER = new ModuleReader() { + private static final ModuleReader NOOP_READER = new ModuleReader() { @Override public Optional find(String name) throws IOException { return Optional.empty(); diff --git a/src/main/java/net/minecraftforge/securemodules/SecureModuleFinder.java b/src/main/java/net/minecraftforge/securemodules/SecureModuleFinder.java index a3712dc..9ecc0e2 100644 --- a/src/main/java/net/minecraftforge/securemodules/SecureModuleFinder.java +++ b/src/main/java/net/minecraftforge/securemodules/SecureModuleFinder.java @@ -80,13 +80,7 @@ public CodeSigner[] getCodeSigners(String entry, byte[] data) { } } - private static class Reader implements ModuleReader { - private final SecureJar.ModuleDataProvider jar; - - public Reader(final SecureJar.ModuleDataProvider jar) { - this.jar = jar; - } - + private record Reader(SecureJar.ModuleDataProvider jar) implements ModuleReader { @Override public Optional find(final String name) throws IOException { return jar.findFile(name);