MinecraftForge / ForgeFlower

Forge's modifications to FernFlower. Fixing various bugs/inconsistencies. Main Repo: https://github.com/MinecraftForge/FernFlower
Apache License 2.0
80 stars 44 forks source link

Fix try with resources being created when it shouldn't be #128

Closed jaskarth closed 1 year ago

jaskarth commented 1 year ago

Fixes a bug in the try with resources processor that caused it to be too lenient when matching the bytecode pattern, by making sure that the catch type is Throwable and that if no close() is found then it would fail, rather than attempting to validate an empty set.

1.19.2 diff:

diff -r -u3 -N a/net/minecraft/util/DirectoryLock.java b/net/minecraft/util/DirectoryLock.java
--- a/net/minecraft/util/DirectoryLock.java 1970-01-11 17:58:04.000000000 -0500
+++ b/net/minecraft/util/DirectoryLock.java 1970-01-11 17:58:04.000000000 -0500
@@ -23,7 +23,9 @@
          Files.createDirectories(p_13641_);
       }

-      try (FileChannel filechannel = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
+      FileChannel filechannel = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
+
+      try {
          filechannel.write(f_13634_.duplicate());
          filechannel.force(true);
          FileLock filelock = filechannel.tryLock();
@@ -32,6 +34,14 @@
          } else {
             return new DirectoryLock(filechannel, filelock);
          }
+      } catch (IOException ioexception1) {
+         try {
+            filechannel.close();
+         } catch (IOException ioexception) {
+            ioexception1.addSuppressed(ioexception);
+         }
+
+         throw ioexception1;
       }
    }